Dot Net/Wcf/Sql server

Friday, 2 September 2016

Cannot redirect after HTTP headers have been sent.

This is an unhanded exception shown may be shown due to the following reason 

1: some times we write code like this 
            .....
            ......
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.   
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = mimeType;
            Response.AddHeader("content-disposition", "attachment; filename= filename" + "." +                         extension);
            Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file  
            Response.Flush(); // send it to the client to download  
            Response.End();

suppose after completing this i want to redirect to another page, so i have to write Response.Redirect("Home.aspx"); 
or suppose we write the above code like this 
           try
          {
              .................................................
                        Response.Buffer = true;
            Response.Clear();
            Response.ContentType = mimeType;
            Response.AddHeader("content-disposition", "attachment; filename= filename" + "." +                         extension);
            Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file  
            Response.Flush(); // send it to the client to download  
            Response.End();
              ...
             ......
             .....
           }
          catch()
         {
            Response.Redirect("ErrorPage.aspx"); 
          }
suppose after executing Response.Flush(); code an exception occur the it will create second response, in both the cases you will get the same error.With HTTP you get one response for one request. Your browser requested the page only once,  
                    Response.Flush(); //First Response
                    Response.Redirect("mithunpatra.blogspot.com"); //Second Response


2: Suppose you have a button control and you want to generate a large report on clicking the button. To generate the report we have redirect it to another page. Now suppose user click on the button two times in a row that means it will generate  two request, In slow network before processing the fast request second request come but HTTP get only one request and one response that time you can get the above error. to solve this error you can use this code
              if (Response.IsRequestBeingRedirected =false)
                     {
                       response.redirrect("mithunpatra.blogspot.com");
                     }

with this code we can ensure one response for one request


Monday, 29 August 2016

Calling a web service To a Client


Creating the Proxy
before using web service to the client a proxy must be created which is registered with the client application.Then the  client application makes the calls to the web service as it were using a local method.The proxy takes the calls, wraps it in proper format and sends it as a SOAP request( Learn about SOAP Click here) to the server. This protocol is used for exchanging web service data.When the server returns the SOAP package to the client, the proxy decodes everything and

presents it to the client application.This Process is describe in this picture

















Now take the following steps for creating the proxy:
Step (1) :In my case, client is a windows application. so to add web reference first right click on application then select add service reference, Now Click on Advance it will open up service reference settings there at the end of the window click add web reference.


















Step (2) : Now add web reference window will open and you have to copy the hosted web service address to the URL address bar. A meaning full web service reference name should be given then click on add reference. It will add the service reference with the client application now from the client application you can create object of the service reference and call the web service method.

Step (3) :  write down the below code to create object of the service .
                  ConsumeService.Service1  obj = new ConsumeService.Service1();

Consuming a web service To a Client

Consuming a web service

Main purpose of creating a web service is to use the service from same server different application or different server . So to access a web service from another application the following task needed to be performed. To know how to create a web service click here.

  •  We have to add the service reference to our client application 
  •  Give the reference a meaning full name 
  •  Create a object of the service 

Creating the Proxy

before using web service to the client a proxy must be created which is registered with the client application.Then the  client application makes the calls to the web service as it were using a local method.The proxy takes the calls, wraps it in proper format and sends it as a SOAP request( For details about SOAP Click here) to the server. This protocol is used for exchanging web service data.When the server returns the SOAP package to the client, the proxy decodes everything and
presents it to the client application.This Process is describe in this picture

Now Take the following steps for creating the proxy:
Step (1) :In my case, client is a windows application. so to add web reference first right click on application then select add service reference, Now Click on Advance it will open up service reference settings there at the end of the window click add web reference.


Step (2) : Now add web reference window will open and you have to copy the hosted web service address to the URL address bar. A meaning full web service reference name should be given then click on add reference. It will add the service reference with the client application now from the client application you can create object of the service reference and call the web service method.



Step (3) :  write down the below code to create object of the service .
                  ConsumeService.Service1  obj = new ConsumeService.Service1();

Saturday, 27 August 2016

Basic of Web service

What is Web Service ?

A Web service is a software system identified by a URI, whose public interfaces and bindings are defined and described using XML ( Learn Details of XML  )
. Its definition can be discovered by other software systems.These systems may then interact with the Web service in a manner prescribed by its definition, using XML based messages conveyed by internet protocols
Web service architecture picture is shown below:



 Web Services are platform independent. Service written in any language can communicate with service of other languages. Web service support  the following  transporting protocol Internet Protocol,HTTP, STMP. Web service uses XML as common language  ( Learn Details of XML  ) to communicate over network between two different application. As a result, the set of data types Web Services can use is limited to the set of data types recognized by the XML Schema ( Learn Details of XML  )standard. So using service we can transport simple data (strings and numbers, object,arrays) and we can't send .Net object like FileStream, an Image. If you want to transport .net( like FileStream, an Image) object to the client then client should be in .net platform.

Web service supported data types are
• Primitive data types (The Basics): The built in C# data types such as short, int, long, ushort, uint,          ulong, float, double, decimal, bool, string, char, byte and DateTime.
• Classes and structure: The Classes and structure with serialize public fields or property.
• Arrays: Arrays of any supported type (built-in or custom objects). You can also use an ArrayList         (that is simply converted into an array).
• Enum types: Enums are supported.
• XmlNode: Xmlnode types can be passed as a parameter or return values.
• DataSet : DataSet are allowed. .

Now To create a web service using visual studio do the following steps:

First open a visual studio and create a web site project. If you are using VS2010 or above then Change the framework version to 3.5. Now add the Web service with a name. This picture will give the details information how to do the above functionality.


Create A web service in a web application

After creating web service it will generate automatically a code behind file which shown in below picture

The MyService class has a method HelloWorld that is decorated with a “[WebMethod]”         attribute. The methods of the service that are to be accessed by the client  application should be decorated with this attribute. There may be some method that the service is using for some internal functionality, client applications don't need to access them. Don't decorate such methods with a WebMethod attribute.
Instead of using Project if you create a website then with in web service asmx page you will find the below tag. Using both the way you can create Web service.
1. <%@ WebService Language="VB" CodeBehind="~/App_Code/MYService.vb" Class="MYService" %>

• The “WebService” directive, indicates this asmx page is a web service.
• The “Language="C#"”, is to indicate language used for this service.
• The “CodeBehind” property is nothing to do with ASP.NET or web service, this is completely a Visual Studio property, that is used to map the asmx page with it's code behind page.


Now, run your application by hitting F5, http://..../MYService.asmx will open in your browser (the port number may vary). On the top of the below image you will find a  link "click here for complete list of operation", that will redirect to the WSDL document ( Learn Details on WSDL ) of the service, another link for HelloWorld (list for methods exposed by service) that will redirect to a page  for testing this method (In this picture the localhost tab containing the result details.).



Thursday, 18 August 2016

Your Name:
E-mail Address *:
Message *: