Accessing Methods Asynchronously in WCF 4.5
Posted by: Pravinkumar Dabade
in Category Windows Communication Foundation (WCF)
Abstract: In this article, we will seen how we can implement Asynchronous Logic in our WCF 4.5 Service and how we can access the WCF methods Asynchronously by generating proxy code using Task-based Programming model.
In a previous article, I demonstrated Intellisense Support and Improved Easier Configuration in Windows Communication Foundation (WCF) 4.5. In this article, we will see how we can implement Asynchronous Logic in our WCF Service as well as how we can access the WCF methods Asynchronously by generating proxy code.
WCF 4.5 has introduced a new way of implementing asynchronous logic in our WCF Services using Task based programming model. To demonstrate examples, I am using Windows 7 and Visual Studio 2012.
Step 1: Create a blank solution with the name WCFAsyncProgramming as shown below -
Step 2: Now add a class library with the name "SimpleWCFAsyncService" in our solution as shown below -
Step 3: Let's add the reference to a library "System.ServiceModel.dll" as shown below -
Step 4: Add an Interface with the name "IHelloService" in our class library, import the namespace System.ServiceModel and add the code as shown below -
The above operation contract does not return "String" but is returning Task<string>. This is a new way of implementing Task-based Asynchronous Programming Model.
Step 5: Add some code to this service where we will implement the above service contract . Rename the "Class1" to "HelloService" and add the following code in our class -
Step 6: Now let's add a Console Application in our solution which will host our service with name "Host". Also add a reference to our WCF Service as well as to the System.ServiceModel.dll -
Step 7: Import a namespace System.ServiceModel and write the following code to host our service -
Step 8: Now it's time to configure our service. Let's configure the service with Basic Http Binding as shown below [Add this code into existing App.Config file of our Console Application]-
Hit "F5" and test your Host application. It should show the following output -
Let's add a Windows Forms Application to test our service and all the service method asynchronously -
Step 9: Add a Win Form application with the name "Client1". Now add a service reference into our client1 application and use the service.
Step 10: To add a service reference, copy the base address from host application configuration file and add a service reference with the name "HelloProxySercice" as shown below - [Before adding a service reference make sure your host application is running.]
Step 11: Now add a List Box with the name lstLoop, a Label with the name lblServiceResult and add two buttons with the text property as "Run Local Loop" and "Call Service" as shown below -
Step 12: Add proxy code into our win form application as shown below -
On button "Call Service" click event, we will first write some code which will call the service method synchronously. When this method is executing, we will test if we are able to do some action on our UI like click the button "Run Local Loop"? The answer is NO. Because it is obvious that we are calling the service method synchronously. Let's Test -
Till the service method does not send the output, you cannot click button "Run Local Loop". The UI becomes completely unresponsive.
Now let's add the following code that calls our service method asynchronously and makes our UI responsive. For Asynchronous call, we will have to write the service method call in a separate method which will be "async" Method.
Make a note that "Run Local Loop" button code will remain the same. Now test the code like you did earlier and you will see that your "Run Local Loop" button is now responsive and clicking it will fill up the list box before the service returns its output to our UI -
Before Service Method returns the output -
After Service Method returns the output -
We can now make our service calls on a background thread which leaves our UI responsive to do other actions. Awesome!!
In this article, we have seen how we can implement Asynchronous Logic in our WCF 4.5 Service and how we can access the WCF methods Asynchronously by generating proxy code using Task-based Programming model.
Download the entire source code here (Github)