Click here to Skip to main content
14,981,383 members
Articles / Programming Languages / C#
Posted 30 Jun 2013

Tagged as


32 bookmarked

Task-based Asynchronous Operation in WCF

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
30 Jun 2013CPOL2 min read
This article describes the new feature of WCF "Task-based asynchronous operation"


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 await and async. But what do async and await do? async and 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 await keyword, 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
  • IAsyncResult 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.

Service Hosting

class Program
    static void Main(string[] args)
       var svcHost = new ServiceHost(typeof (MessageService));
       Console.WriteLine("Available Endpoints :\n");
        (endpoints=> Console.WriteLine(endpoints.Address.ToString()));

Service Configuration

<?xml version="1.0"?>
          <service name="Rashim.RND.WCF.Asynchronous.ServiceImplementation.MessageService">
                     <add baseAddress="net.Tcp://localhost:8732/"/>
                     <add baseAddress="http://localhost:8889/"/>
             <endpoint address="Tcp" binding="netTcpBinding" 
             <endpoint address="Http" binding="basicHttpBinding" 
                      <dns value="localhost"/>
             <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
     <startup><supportedRuntime version="v4.0" 

After configuring the service, we need to configure the client app to consume the service.

Define Client

A simple Console Application (Client):

class Program
    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;
       str.ContinueWith(e =>
          if (e.IsCompleted)
      Console.WriteLine("Waiting for the result");

Client Configuration

<?xml version="1.0"?>
         <binding name="BasicHttpBinding_IMessage" />
       <endpoint address="http://localhost:8889/Http" binding="basicHttpBinding"
           name="BasicHttpBinding_IMessage" />

Finally, proxy class is given below through which the client will consume the services.

public class Proxy : ClientBase<IMessage>, IMessage
    public Proxy()
    public Proxy(string endpointConfigurationName) :
    public Task<string> GetMessages(string msg)
      return Channel.GetMessages(msg);

That’s it. Very easy stuff though.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Rashim uddin
Software Developer (Senior) KAZ Software Limited.
Bangladesh Bangladesh
I am a true visionary as well as have always been passionate about learning. I always love to work with thought-provoking personnel to develop amazing, exciting and innovative ideas as part of an aggressive team that challenges my ingenious ideas, creativeness and extensive experience.

My Blog:

My Email: rashimiiuc at yahoo dot com

Comments and Discussions

PraiseThank you. Pin
Dimitri7C31-Jul-17 4:26
MemberDimitri7C31-Jul-17 4:26 
AnswerAlmost good Pin
alexandis6-Nov-16 2:11
Memberalexandis6-Nov-16 2:11 
GeneralMy vote of 3 Pin
Manas_Kumar5-Nov-15 3:34
professionalManas_Kumar5-Nov-15 3:34 
Questionwhat type is the variable str ? Pin
Mohamed hammad27-May-15 19:29
MemberMohamed hammad27-May-15 19:29 
Questionhow the client method knows Service method is completed Pin
AshleyAlex27-Nov-14 0:59
MemberAshleyAlex27-Nov-14 0:59 
SuggestionThread.Sleep(10000); Pin
Member 1063546728-Sep-14 21:55
MemberMember 1063546728-Sep-14 21:55 
Questionhmmm Pin
Sacha Barber9-Sep-14 11:34
mvaSacha Barber9-Sep-14 11:34 
GeneralDecent effort Pin
Karen Payne11-Aug-14 10:00
MemberKaren Payne11-Aug-14 10:00 
GeneralMy vote of 2 Pin
svshivshan2-Jul-14 21:42
Membersvshivshan2-Jul-14 21:42 
QuestionOne query Pin
Tridip Bhattacharjee9-Feb-14 19:31
professionalTridip Bhattacharjee9-Feb-14 19:31 
AnswerRe: One query Pin
Md. Rashim uddin11-Feb-14 19:52
MemberMd. Rashim uddin11-Feb-14 19:52 
GeneralMy vote of 3 Pin
sinhar20-Nov-13 8:29
Membersinhar20-Nov-13 8:29 
QuestionWindows Phone Pin
MoritzM21-Sep-13 11:36
MemberMoritzM21-Sep-13 11:36 
AnswerRe: Windows Phone Pin
Md. Rashim uddin21-Sep-13 21:17
MemberMd. Rashim uddin21-Sep-13 21:17 
GeneralRe: Windows Phone Pin
MoritzM1-Oct-13 0:04
MemberMoritzM1-Oct-13 0:04 
GeneralMy vote of 5 Pin
Monjurul Habib14-Jul-13 3:33
professionalMonjurul Habib14-Jul-13 3:33 
GeneralRe: My vote of 5 Pin
Md. Rashim uddin14-Jul-13 4:44
MemberMd. Rashim uddin14-Jul-13 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.