Accessing Methods Asynchronously in WCF 4.5

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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

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.