DotNetCurry Logo

Accessing Methods Asynchronously in WCF 4.5

Posted by: Pravinkumar Dabade , on 12/19/2012, in Category Windows Communication Foundation (WCF)
Views: 31906
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 -

wcf-async-01 

Step 2: Now add a class library with the name "SimpleWCFAsyncService" in our solution as shown below -

wcf-async-03

Step 3: Let's add the reference to a library "System.ServiceModel.dll" as shown below -

wcf-async-05

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 -

wcf-async-07

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 -

wcf-async-09

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 -

wcf-async-11

Step 7: Import a namespace System.ServiceModel and write the following code to host our service -

wcf-async-13

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]-

wcf-async-15

Hit "F5" and test your Host application. It should show the following output -

wcf-async-17

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.]

wcf-async-21

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 -

wcf-async-22

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 -

wcf-async-24

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.

wcf-async-26

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 -

wcf-async-28

After Service Method returns the output -

wcf-async-30

We can now make our service calls on a background thread which leaves our UI responsive to do other actions. Awesome!!

Conclusion

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)

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Joydip Roychowdhury on Saturday, January 5, 2013 4:43 AM
It is better to have more elaboration on the topics you are harping on
Comment posted by ariel on Wednesday, July 31, 2013 9:06 PM
Nice example. Short and clear.
Thanks.