When Invoking WCF services there comes a time when you need to give up procedural synchronous ways of doing things and use the arbitrary asynchronous ways to do things. This post will show you how this is possible.
Impact:
Make your applications responsive.
Solution:
The .NET framework way back since version 1.0 introduced an asynchronous pattern which enables your code to call any synchronous method asynchronously. Things required to make this happen
- IAsyncResult class
- Begin<SynchronousMethodName>
- End<SynchronousMethodName
namespace Client
{
class Program
{
static int c = 0;
static void Main(string[] args)
{
ServiceReference1.StockServiceClient proxy = new ServiceReference1.StockServiceClient();
//First call it synchronously
double price = proxy.GetPrice("msft");
Console.WriteLine("{0} Sync price lookup:{1}", System.DateTime.Now, price);
//Then call it 10 times asynchrnonously
for (int i = 0; i < 10; i++)
{
Interlocked.Increment(ref c);
proxy.BeginGetPrice("msft", PriceCallback, proxy);
}
while (c > 0)
{
Thread.Sleep(1000);
}
proxy.Close();
Console.WriteLine("Done!");
}
// Asynchronous callbacks for displaying results.
static void PriceCallback(IAsyncResult ar)
{
double price = ((ServiceReference1.StockServiceClient)ar.AsyncState).EndGetPrice(ar);
Console.WriteLine("{0} Asynchronous price lookup:{1}", System.DateTime.Now, price);
Interlocked.Decrement(ref c);
}
}
}
As you can see in the code above the service contains GetPrice() method that the client uses to invoke through the proxy object. But guess when you can invoke this method Asynchronously without using a single line of threading code in either the client or server code. To do that, first
Go back to your Service Reference, right click it and Update Service Reference and select the Generate asynchronous operations. Now this will generate the Begin<GetUpdate> and End<GetUpdate> methods that can be used to asynchronously call the synchronous GetUpdate method.... how cool is that.
What does this do?
proxy.BeginGetPrice("msft", PriceCallback, proxy);
First you need to pass in any parameters required by the BeginGetPrice method. You also need to pass a
delegate (a method that accepts an IAsync state parameter) and a State object (which is usually the proxy class itself).
The delegate is called whenever the Asynchronous operation begins. For every Begin<SynchronousMethodName> you need to call the End<SynchronousMethodName> and this is usually performed in the delegate that is called when the Asynchronous operation begins.
((ServiceReference1.StockServiceClient)ar.AsyncState).EndGetPrice(ar);
Recall in the proxy.BeginGetPrice() we passed the proxy object as an argument. Withing the delegate you can call this object using the code above. And it is as simple as that. Alright it is not that simple, but give it a try the next time you need to create your own asynchronous client without working with threads.
Since I am just a student (and NOT a bright one) I am bound to make a lot of mistakes. Please let me know if you find mistakes, or if more explanation is required.
Thank You Very Much For Your Time And I Hope It Was Time Well Spent

No comments:
Post a Comment