namrata's profileNashaPhotosBlogListsMore Tools Help

Blog


    December 23

    Article : WebServices Series Part - 5 ( Sync & Async Communication )

    Yesterday we saw how we can communicate with web services using callbacks. Today we will discuss the other two techniques  Polling and Waithandles.

     
    Polling :

    When we made a call to our web service using callback we passed it reference to our fucntion and in return we get an IAsyncResult  object back which acted as a token.

    In case of polling we will again call BeginCoSine method but not pass any parameter for callback. This method will return us an  IAsyncResult object. Polling works on one of the properties of this object called IsCompleted. After calling the method we will have to  continuous POLL for the value of this property. When the webservice is completed with its processing the value IAsyncResult object  will be set to true.After that we call the EndCoSine function passing it our IAsyncResult object (which acts as a token) to retrieve our  result.

    Add another button to your windows form and put in the below code :

                    private void button3_Click(object sender, System.EventArgs e)
                    {
                            int noOfSec = 0;
                            oIAsyncResult = trigoService.BeginCoSine(90,null,null);
                            while(oIAsyncResult.IsCompleted == false)
                            {
                                    noOfSec++;
                                    Thread.Sleep(1000);
                            }
                            MessageBox.Show("Completed in " + noOfSec);
                            label1.Text =  Convert.ToString(trigoService.EndCoSine(oIAsyncResult));
                    }


    Here we are checking out the value of IsCompleted property in a while loop. After the value is set to true we message the number of  seconds that were taken to POLL. Finally we call EndCosine function passing our IAsyncResult object.

    WaitHandles :

    This method also uses one of the properties of IAsyncResult object called AsyncWaitHandle.

    This approach is generally used when u dont want to leave the thread in which you are communicating with the webservice. Thus in  this case you will wait for the webservice to complete its work without releasing the thread in which you were communicating. You can achieve this by using the WaitHandle returned by AsyncWaitHandle property of IAsyncResult object.

    After you have called the BeginCosine method and got your IAsyncResult Token object you can use its AsyncWaitHandle property to  get WaitHandle object then use its WaitOne method to wait for the thread. WaitOne has multiple overloads some also take the  amount of time you want to wait.

    Add another button to your windows form and add the following code snippet

                    private void button4_Click(object sender, System.EventArgs e)
                    {
                            int noOfSec = 0;
                            oIAsyncResult = trigoService.BeginCoSine(90,null,null);
                            oIAsyncResult.AsyncWaitHandle.WaitOne();
                            while(noOfSec++ < 5)
                            {
                                    noOfSec++;
                                    Thread.Sleep(1000);                            
                            }
                            MessageBox.Show("Waited for " + noOfSec);                      
                            label1.Text =  Convert.ToString(trigoService.EndCoSine(oIAsyncResult));        
                    }

    You can also checkout the other static methods of the WaitHandle object like WaitAll and WaitAny. These methods are particularly  used when you have multiple web service calls and you want to wait for either ALL of them to complete their processing and then  move ahead or with ANY ONE of them to complete their processing and then move ahead.

    WAIT ALL :- Wait for all the web services to complete processing.
    WAIT ANY:- Wait for any one of them to complete processing.

    Article : WebServices Series Part - 4 ( Sync & Async Communication )

    Today we will checkout how to consume our web service using SOAP in Synchronous and Asynchronous modes.

    To consume to our web service we need to create our web service client.

    Let us go ahead and create our web service client.

    Open a new windows application and Select References Right click and select Add Web Reference. Select our Trigonometry web service.

    WebReference to this web service will be added and along with it you will notice the reference.map , .disco and .wsdl files will be added for the web service. Our client interacts with our web service using a proxy class.

    To view the proxy class ... Select the solution in the Solution Explorer and click on Show All Files ... You will notice a file under the Reference map node called as Reference.cs.

    Open the Reference.cs file and Checkout the BASE CLASS for our webservice proxy class, it should be SoapHttpClientProtocol.

    You should also notice that for each method in our webservice there are three methods in the webservice proxy class.

    For Example these are the the three methods for Sine.

     public System.Double Sine(System.Double angel)
     public System.IAsyncResult BeginSine(System.Double angel, System.AsyncCallback callback, object asyncState)
     public System.Double EndSine(System.IAsyncResult asyncResult)

    Out of these three the first one is used for Synchronous communication and latter two for Asyncrhomous communication.

    Let us checkout Synchrounous Communication.

    Add a Form and add a button and a label to that Form

    Declare a class level variable for our webservice

                    localhost.Service1 trigoService = new localhost.Service1();

    Write the following code in the button Click event

                    private void button1_Click(object sender, System.EventArgs e)
                    {
                            label1.Text = Convert.ToString(trigoService.Sine(90));
                    }

    Execute the apllication and checkout the results.

    One of methods of Asynchronous communication with web service is using Callbacks.

    To start with declare a class level variable for IAyncResult 

            private IAsyncResult oIAsyncResult;

    To consume our web service in asynchronous mode let us add another button to our form and add the below code. When we communicate with our web service in a asynchronous mode we will have to call the BeginSine method and pass a CALLBACK function to it. After our web service is done with its processing it will callback our function to return the result.

                    private void button2_Click(object sender, System.EventArgs e)
                    {                      
            oIAsyncResult = trigoService.BeginSine(90,new AsyncCallback(CallBackForEndSine),null);
                    }

                    public void CallBackForEndSine(IAsyncResult oIAsyncResult)
                    {
                            double sineVal = trigoService.EndSine(oIAsyncResult);                  
                            label1.Text = Convert.ToString(sineVal);
                    }

    Here I am calling the BeginSine method and passing it the reference of our CallBack method CallBackForEndSine. You should notice that on Calling the BeginSine method I am returned an IAysncResult object. This object acts as a TOKEN between the client and the webservice. Hence when the Webservice is through with its processing and Callback method is called, to get the result from the webserivce we need to call EndSine method in the CallBack method passing IAsyncResult Token.

    The Difference between Asynchronous  & Synchronous consumption of webservices can be noticed when there is a lot of processing done by a webservice. Thus if our webservice is consumed in a synchronous mode then our clients will have to wait for the processing to complete.

    To get a good taste of Asynchronous processing with our webservice we will make a little change in our methods. Before returning the result we will make the thread sleep for 20 secs.  


                    public double Sine(double angel)
                    {
                            Thread.Sleep(20000); // make a similar change in all the methods
                            return System.Math.Sin(angel);
                    }

    Compile the webservice. Update the webservice reference in your Client project and Execute your Client.

    Now if our web service is consumed synchrounsouly our client will have to wait for the result. Click on Button1 and check it out. The control will come back after 20 secs and till then your application will not respond to any other events. Now let us consume the webservice in an ansychronous mode by clicking on Button2. Since the communication is asynchronous the control will return back immediately rather than waiting for 20 seconds. Later the call back function will be called to deliver the results.

    WebServices Series Part - 3 (Accessing with HTTP POSTLOCALHOST, POST and GET)

    Today we will look how we can access web services with HTTP-GET and HTTP-POST protocols. 

     .Net Framework 1.0 had all the three protocols i.e. HTTP-POST,HTTP-GET AND HTTP-SOAP enabled by default. But in version 1.1 only HTTP-SOAP is enabled by default. Hence if you want to access the web service my HTTP-POST or GET then you will have to enable it explicitly.

     Since HTTP-POST is not enabled by default in .Net framework 1.1 to test web service on the local machine there is a new protocol that has been added i.e. HTTPPOSTLOCALHOST. It is because of this protocol that while testing a web service you are able to view a test form on your local machine and not on a remote computer.

    Let us check out each of these protocols individually: -

    HTTPPOSTLOCALHOST: - This is enabled by default in .Net Framework version 1.1. Hence if you access your website as http://localhost you will get the test form but if you test the same web service with http://<name of your computer> from some other computer than the test form will not be displayed.

    e.g. Try and access our trigonometry web service from your machine and then access it from any other machine. You will notice that the test form will not be displayed.

    Before we look at HTTP-POST and HTTP-GET we need to checkout DefaultWsdlHelpGenerator.aspx.

    DefaultWsdlHelpGenerator.aspx is the default wsdl help file located at

    <drive>:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG.

    Open DefaultWsdlHelpGenerator.aspx and checkout showPost flag.

        bool showPost = true; // search for this

    Depending the value of showPost flag HTTP-GET or HTTP-POST  protocol is used.Set its value to true if you want to see a POST test form and false to view  a GET test form.

    Along with this you will have to include the appropriate protocols in the web services tag in Web.config.

    HTTP-POST: - To enable our web service with HTTP-POST we will have to make the following changes.

    1. Open the web.config and add the <webServices> tag under <system.web> as follows to enable the HTTP-POST protocol.

         <webServices>
             <protocols>
                <add name="HttpPost"/>
             </protocols>
          </webServices>

    2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to true. By default it is set to true.
    3. Run the web service and checkout the HTTP-POST form.
    4. On execution you will see the xml output of your webservice. Checkout the URL of output in the browser it will not show any querystring parameters.

     http://namrathas/Trigonometery/MyService.asmx/CoSine

    5. Execute your web service from some other computer and you will be able to view HTTP-POST test form.

    HTTP-GET: - To enable our web service with HTTP-GET we will have to make the following changes.

    1. Open the web.config and add <add name="HttpGet"/>  to protocols tage s follows to enable our webservice with HTTP-GET protocol also.

         <webServices>
             <protocols>
                <add name="HttpPost"/>
                <add name="HttpGet"/>
             </protocols>
          </webServices>

    2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to false. So now HTTP-POST form will be displayed. All the parameters will be passed as querystring.

     http://namratas/Trigo/MyService.asmx/CoSine?angel=5

    3. Run the web service and checkout the HTTP-GET form. Its looks the same as HTTP-POST form. You will notice the difference on execution.
    4. Execute the web service and you will see the xml output of your webservice. Checkout the URL of output in the browser it will show query string parameters passed to the web method.

    HTTP-SOAP: - Whenever our webservice is accessed from any application HTTP-SOAP protocol is used. HTTP-SOAP is enabled by default. We will look in to more details of HTTP-SOAP when we look into consuming webservices.

    Hence if want our webservice to be enabled for all the four protocols we need to add them in the <protocols> tag.

         <webServices>
             <protocols>
                <add name="HttpPost"/>
                <add name="HttpGet"/>
                <add name="HttpSoap"/>
                <add name="HttpPostLocalhost"/>
             </protocols>
          </webServices>

    WebServices Series Part - 2 (WebMethod and WebService Properties)

     After creating our basic web service yesterday we will look into the web method properties and supported data types.

    Well Whenever we create a web service all those methods of the web service which we want to be web-callable have to be marked with the web method attribute and should have a public access modifier. Web Method Attribute is basically used to make a method web callable.

     WebMethod attribute has the following properties like :-

    • Description
    • TransactionOption
    • CacheDuration
    • BuffferResponse
    • EnableSession
    • MessageName

     

    Description :- It is used for commenting the web method. The description that is added here is included in WSDL. Add the

    webmethod description property above any method as shown below. When you call the webservice from a browser you view the web service.

     

     e.g. [WebMethod(Description = "My WebMethod Description comes here")]

     

    Enable Session :- WebServices are a type of ASP.NET applications hence you can access your ASP.NET application Session from your webservice. By default the session is disabled i.e. set to false. You can enable it by setting its value to true.

     

     e.g. [WebMethod(EnableSession ="true")]

     

    Message Name :- Message Name property is used give an alias to a method name. This is esp. useful when you are using overloaded methods.

     

    e.g. Yesterday we created  3 method in our webservice for cos,sin and tan which took double as an input parameter. Today we will create their overloads that will take int as an input parameter

     

      [WebMethod]
      public double Tan(int angel)
      {
       return System.Math.Tan(angel);
      }
      [WebMethod]
      public double Sine(int angel)
      {
       return System.Math.Sin(angel);
      }
      [WebMethod]
      public double CoSine(int angel)
      {
       return System.Math.Cos(angel);
      }

     

    If you compile this code it will compile successfully. It is on execution that you we view the below error message

     

    Error Message: Both Double Tan(Int32) and Double Tan(Double) use the message name 'Tan'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

     

    Web service needs to identify each method uniquely hence we will give alias to the methods by setting the MessageName property.

     

     e.g. [WebMethod(MessageName = "SinInt")]
           [WebMethod(MessageName = "CosInt")]
           [WebMethod(MessageName = "TanInt")]

     

    TransactionOption : - If our web service is a part if any COM/MTS transaction application then we can enable the Transaction Option of the webservice thus making it a part of the  2 phase commit protocol of MTS.

     

     e.g.     [WebMethod(TransactionOption = "Supported")]

     

    The dafault is same as COM/MTS transactions i.e. Required.

     

    CacheDuration :- Like any other ASP.NET applications WebServices also support output caching. Output caching gives a performance boost coz the same request is not executed repeatedly instead the result is return from the cache itself. This feature is

    esp. useful when you are retrieving chunks of data from the database for listing and other purposes. But caching should preferably used with only those methods where we know that the data will not change frequently.

     

     e.g.     [WebMethod(CacheDuration = "180")] 
     
    This means the output of this method will be cached for  seconds.

     

    BuffferResponse :- We all are familiar with the buffering property rite from our ASP days where used Response.Buffer = true so that

    the response is only sent when the Response for the request is completely generated or when the buffer is full. WebServices also use the same logic. If we want our response to be sent across only after its complete processing is done we need to buffer our response by setting its property to true.

     

     e.g.     [WebMethod(BufferResponse = "true")] 

     

    By default it is set to true.

     

    These properties were webmethod properties. We also have some web service properties like

     

    • Description.
    • Name
    • NameSpace
       

    Description :- This is same as web method property. It is used to comment web service and give it a description that is included in WSDL.

     

     e.g. [WebService(Description = "My WebService Description comes here")]
      public class MyService : System.Web.Services.WebService

      {
    Name :- By default the name of the class is used as the service name but incase we want to give a different service name which will to exposed to our clients and in WSDL we can set the Name property. This property will override the default value i.e. the class name.

     

     e.g. [WebService(Name = "MyNewName")]
      public class MyService : System.Web.Services.WebService
      {

     

    NameSpace :-  The default name space is http://tempuri.org. You can change this to http://localhost/MyWebServices. You should set namespace to any unique value.

     

     e.g. [WebService(Namespace = "http://localhost/MyWebServices")]
      public class MyService : System.Web.Services.WebService
      {

    WebServices Series Part - 1 [ Basic Creation ]

    Web services are distributed asp.net applications that are designed to share business logic over the network.
    Simplest way to make a web service is to inherit your class from web service base class although it is possible to create a web service, which does not inherit from it. You can communicate with a web service using HTTP -GET, HTTP-POST or SOAP protocols.

    Web services are about discovery, description and prototcols. UDDI and DISCO specifications are used to discover a web service.  WSDL i.e. web service description language is used to describe web service. Clients that communicate with web services use WSDL to create their web service proxies.

    Lets today just go ahead and build our web service.

    1. Open a new ASP.NET web service project.
    2. Name it as Trigonometry.
    3. Rename Service1 as MyService.asmx
    4. Add the following methods to our service.

                    public double Sine(double angel)
                    {
                            return System.Math.Sin(angel);
                    }

                    public double CoSine(double angel)
                    {
                            return System.Math.Cos(angel);
                    }

                    public double Tan(double angel)
                    {
                            return System.Math.Tan(angel);
                    }


    5. Compile and service and run it in the browser. You will notice none of the methods that we have written are visible.
    6. Stop the application and go back and put the [WebMethod] attribute above each the methods.
    7. Rebuild and execute the service again.
    8. Execute all the methods and see that our web service is working.

    To expose any method in a web service it is important to add [WebMethod] attribute to it. Tomorrow we will checkout the various PROPERTIES of the [WebMethod] attribute and also see how we can consume our web service by using different protocols.