Thursday, December 20, 2012

Calling WCF service methods asynchronously in C# Application


The WCF service methods can be called asynchronously. The main benefit of calling the wcf methods asynchronously is to avoid unnecessarily waiting by the application for the response from the service. This will also help to avoid the application from hanging state. This is also good from usability prospective. All you need is while consuming the service; client has an option whether to generate classes for handling calls asynchronously or not. So follow the following steps if you want to call the methods asynchronously

1.       Open the Solution Explorer
2.       Right Click on serve reference and Click Add Service Reference..
 

3.       Add Service Reference window will be opened
4.       Click on Advanced Button and Service Reference Settings window will open
5.       Check the option  Generate asynchronous operations and Press Ok

 

6.       Add the wcf url in address and press Ok to generate the proxy calls with auto generated  asynchronous methods
7.       Now Open a Class where you want to call wcf methods
8.       Add the Namespace for the service
9.       Create the proxy object of the service as

TestServiceClient  client = new TestServiceClient(); 

10.   Define the delegates only once in the constructor of the Class as 

client.CreateEntityCompleted += new EventHandler<CreateEntityCompletedEventArgs>(client_CreateEntityCompleted);

Note: CreateEntity is an remote method exposed by the service. This delegate will call automatically once the service returns the response.

11.   Definition of the delegate will be created automatically once you declare the above statement as below
           void client_CreateEntityCompleted(object sender, CreateEntityCompletedEventArgs e)
       {
            } 

12.   Call the asynchronous method of the service as below

client.CreateEntityAsync(Obj, 1);

CreateEntity is the actual method created in the wcf code but CreateEntityAsync is created while consuming the service to call the wcf methods asynchronously. The service will call this method asynchronously and once the service returns response the above delegate method client_CreateEntityCompleted is called. You application will not hang till the service returns the response.

13.   Now in order to handle any kind of error or result use the following code in completion delegate method code
 
        void client_CreateEntityCompleted(object sender, CreateEntityCompletedEventArgs e)
       {
//check of there is any error happened
                       if (e.Error != null)
              {
                if (e.Error is FaultException)
                {
                    FaultException ex = (FaultException)e.Error;
                    MessageBox.Show("Error : " + ex.Code.Name + "\nReason : " + ex.Reason);
                }
                else
                {
                    MessageBox.Show("Error: " + e.Error.ToString());
                }
            }
            else if (e.Result)
            {
                MessageBox.Show(e.Result);
            }
            else
            {
                MessageBox.Show("Request is failed !!!!!!");
            }
            }

 

No comments:

Post a Comment