Asynchronous Pattern in Windows Communication Foundation
page 2 of 7
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17865/ 236

Asynchronous Pattern in a Nutshell

Before implementing our asynchronous pattern in WCF, I think it is better to give an introduction to asynchronous pattern and its applications in this section.  Many of you may be partially or completely familiar with this design pattern so you can escape this section and read the next sections directly.

For long running executions in an application, there is a probability that a current thread cannot keep executing because it may stop user interface.  For example, when a Windows application goes to a new state to execute a long running process, windows may freeze and even crash.  One solution is to move this execution to another thread and let it follow there.

Asynchronous pattern comes into play to solve this issue and Microsoft has a built-in mechanism for this pattern in its great .NET Framework.  Microsoft's implementation of this pattern consists of these pieces:

·         Two methods for an asynchronous operation

·         An object which implements IAsyncCallback interface

·         A callback delegate

This way, execution of a method splits into two steps.  In the first step you create the background process and start it and in the second step you listen for changes in process and wait until it finishes.

For example, if we have a method to add two numbers and name it Add() then asynchronous version of this method consists of two methods: BeginAdd() and EndAdd().

If Add() has a signature like this:

int Add(int x, int y);

Then BeginAdd() has this signature:

IAsyncResult BegingAdd(int x, int y, AsyncCallback callback, object state);

As you see, BeginAdd() has two extra parameters in comparison with original Add() method:

·         AsyncCallback: A delegate that gets an IAsyncResult parameter and does not return anything.  It will be called when asynchronous operation is completed.

·         object: It keeps the state of the asynchronous  operation as an IAsyncResult object.

This method also returns an IAsyncResult object rather than the original integer type.

On the other hand, EndAdd() has the following signature:

int EndAdd(IAsyncResult ar);

As you see, this method only has one parameter of IAsyncResult type. This object gives all necessary information about the state of operation and has the following properties.

·         AsynState: Gets an object that provides some information about asynchronous operation.

·         AsycWaitHandler: Gets a WaitHandle to wait for an asynchronous operation to complete.

·         CompletedSynchronously: Indicates if the asynchronous operation has completed synchronously.

·         IsCompleted: Indicates if the asynchronous operation has completed.

Having this background information, we can step into WCF and talk about asynchronous pattern implementation in it which may be a little different than what you have seen in other parts of .NET Framework.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 2 and type the answer here:

User Comments

Title: Thanx   
Name: MC
Date: 2/21/2008 4:02:44 PM
Comment:
Very hepful !
Title: Event-Based Asynchronous Programming   
Name: Nirmala
Date: 11/23/2007 7:47:55 AM
Comment:
Why Event-Based Asynchronous Programming is not supported in WCF proxy? Is there any work around to fill the gap?
Title: InvokeAsync in WCF   
Name: Nirmala
Date: 11/23/2007 7:33:17 AM
Comment:
In WebService proxy, we have InvokeAsync (of SoapHttpClientProtocol).
But, WCF proxy is missing the InvokeAsync.

Will VS 2008/3.5 support InvokeAsync?
Title: Asynchronous Pattern in WCF   
Name: Nirmala Patthi
Date: 11/19/2007 6:02:17 AM
Comment:
Hi Keyvan Nayyeri,

Thanks for your article. Do you have any idea how to implement Async2 patten in WCF? In the dotnet WebServices, SoapHttpClientProtocol has InvokeAsync that implements Async2 pattern (that is newly introduced in asp.net 2.0 framework).
When we create a WebService proxy, it will have the InvokeAsync implemented by default.
However, when we create a WCF proxy, InvokeAsync is not generated, rather we will have BeginInvoke, EndInvoke though.
I am not sure about if dotnet 3.5 framework, if WCF will come with Async2 pattern.
Can you please comment on this?
Title: THX   
Name: Amir Sameni
Date: 7/15/2007 3:21:47 AM
Comment:
thanks for sharing
Title: Thanks   
Name: Keyvan Nayyeri
Date: 7/13/2007 3:19:21 AM
Comment:
Thank you Darren :-)
Title: Nice!   
Name: Darren Neimke
Date: 7/13/2007 3:04:25 AM
Comment:
Nice stuff Keyvan... thanks for sharing :-)

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/9/2009 7:17:47 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search