Introduction:
In this article,i am going to explain about how to create asynchronous WCF client with sample program.
Main:
We don’t have any major difference between asynchronous WCF client and asynchronous webservice client.
Steps Involved for creating async client,
1.Enable asynchronous option,while adding service reference in client program,
2.Create one callback function for setting result,
In article http://netprogramminghelp.com/wcf/how-to-create-and-hostuse-wcf-applications-in-aspnetc-simple-wcf-overviewprogramming/,we already created one sample wcf service,i am going to use the same reference for this demonstration,
Sample Client Program,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using PerformAsync.proxy;
namespace PerformAsync
{
class Program
{
static int pointer = 0; //For Finding process state, 0 refers completed 1 refers processing
static void Main(string[] args)
{
proxy.ArithmaticClient objclient = new ArithmaticClient();
objclient.BeginAddition(10, 20, ResultCallBack, objclient);
Interlocked.Increment(ref pointer);
while (pointer > 0)
{
Thread.Sleep(1000);
Console.WriteLine("waiting for result from server : " + pointer);
}
Console.WriteLine("Done");
objclient.Close();
Console.ReadKey();
}
// ascync callback for displaying result
static void ResultCallBack(IAsyncResult ar)
{
Int32 Result = 0;
((proxy.ArithmaticClient)ar.AsyncState).EndAddition(out Result, ar);
Interlocked.Decrement(ref pointer);
Console.WriteLine("Output For addition is:" + Result);
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Threading; using PerformAsync.proxy; namespace PerformAsync { class Program { static int pointer = 0; //For Finding process state, 0 refers completed 1 refers processing static void Main(string[] args) { proxy.ArithmaticClient objclient = new ArithmaticClient(); objclient.BeginAddition(10, 20, ResultCallBack, objclient); Interlocked.Increment(ref pointer); while (pointer > 0) { Thread.Sleep(1000); Console.WriteLine("waiting for result from server : " + pointer); } Console.WriteLine("Done"); objclient.Close(); Console.ReadKey(); } // ascync callback for displaying result static void ResultCallBack(IAsyncResult ar) { Int32 Result = 0; ((proxy.ArithmaticClient)ar.AsyncState).EndAddition(out Result, ar); Interlocked.Decrement(ref pointer); Console.WriteLine("Output For addition is:" + Result); } } } |
Conclusion:
Hope this helps,
Happy coding.



I found your site via search engine a few moment ago, and luckily, this is the only information I was looking for the last hours