Though performance blocking and sluggishness are the tailbacks for any application, we can easily overcome these bottlenecks by using asynchronous programming. But old-style practice for asynchronous programming is not way easy enough to write, debug and maintain. So what is the contemporary approach??
Well, in my view, this is task based asynchronous programming, which is updated in .NET 4.5 through the use of keywords
async. But what do
await are the way of controlling continuation. When a method uses the
async keyword, it means it is an asynchronous method, which might have an
await keyword inside, and if it has an
async will activate it. So, simply
async activates the
await, from which point, the asynchronous has been started. There is a nice explanation that has been given here.
In WCF, we can also consider an asynchronous operation while the service operation creates a delaying call. There are three ways to implement asynchronous operations:
- Task-based asynchronous
- Event-based asynchronous
In this article, I am going to use task-based asynchronous operation since this is the most contemporary strategy.
Okay. Let’s move to the code.
Define and Implement Service
A very simple Service contract such as:
public interface IMessage
Task<string> GetMessages(string msg);
With this simple contract, the implementation is just straight forward.
public class MessageService : IMessage
async Task<string> IMessage.GetMessages(string msg)
var task = Task.Factory.StartNew(() =>
return "Return from Server : " + msg;
return await task.ConfigureAwait(false);
Here, the method is marked with the
async keyword, which means it might use
await keyword inside. It also means that the method will be able to suspend and then resume asynchronously at
await points. Moreover, it points the compiler to boost the outcome of the method or any exceptions that may happen into the return type.
static void Main(string args)
var svcHost = new ServiceHost(typeof (MessageService));
Console.WriteLine("Available Endpoints :\n");
<endpoint address="Tcp" binding="netTcpBinding"
<endpoint address="Http" binding="basicHttpBinding"
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
After configuring the service, we need to configure the client app to consume the service.
A simple Console Application (Client):
static void Main(string args)
private async static void GetResult()
var client = new Proxy("BasicHttpBinding_IMessage");
var task = Task.Factory.StartNew(() => client.GetMessages("Hello"));
var str = await task;
Console.WriteLine("Waiting for the result");
<binding name="BasicHttpBinding_IMessage" />
<endpoint address="http://localhost:8889/Http" binding="basicHttpBinding"
proxy class is given below through which the client will consume the services.
public class Proxy : ClientBase<IMessage>, IMessage
public Proxy(string endpointConfigurationName) :
public Task<string> GetMessages(string msg)
That’s it. Very easy stuff though.