Define the WCF service as follows:
namespace StackOverflow { [ServiceContract] public interface ITest { [OperationContract] string GetName(); } public class Test : ITest { public string GetName() { return "Joel Spolsky"; } } }
If you run svcutil, you will get the following client definition:
public partial class TestClient : System.ServiceModel.ClientBase<ITest>, ITest {
If you run svcutil again using the / async flag, you will get the following client definition:
public partial class TestClient : System.ServiceModel.ClientBase<ITest>, ITest { // Other details elided... public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted; public string GetData(int value) { return base.Channel.GetData(value); } [EditorBrowsableAttribute(EditorBrowsableState.Advanced)] public System.IAsyncResult BeginGetData(int value, System.AsyncCallback callback, object asyncState) { return base.Channel.BeginGetData(value, callback, asyncState); } [EditorBrowsableAttribute(EditorBrowsableState.Advanced)] public string EndGetData(System.IAsyncResult result) { return base.Channel.EndGetData(result); } public void GetDataAsync(int value, object userState) { if ((this.onBeginGetDataDelegate == null)) { this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData); } if ((this.onEndGetDataDelegate == null)) { this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData); } if ((this.onGetDataCompletedDelegate == null)) { this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted); } base.InvokeAsync(this.onBeginGetDataDelegate, new object[] {value}, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState); } }
Thus, the / async flag simply provides you with the ability to interact with your service asynchronously, rather than the default for synchronous behavior.
The GetDataAsync () method asynchronously calls the GetData () method and notifies you when it is completed through the GetDataCompleted event.
The BeginGetData () and EndGetData () methods use delegate asynchronous behavior to asynchronously call the GetData () method. This is similar to the BeginInvoke () and EndInvoke () methods in the System.Windows.Forms.Control class or BeginRead () and EndRead () in the System.IO.Stream class.
source share