Difference between asynchronous methods based on events and callback / based on delegates?

When using svcutil.exe, I noticed this switch, / tcv: Version35. The docs say this:

Version 35: Use / tcv: Version35 if you are generating code for clients that use the .NET Framework 3.5. Using this value, the SvcUtil.exe tool generates code that references functionality in the .NET Framework 3.5 and previous versions. When using / tcv: Version35 with the / async switch, both event-based and callback / delegate-asynchronous methods are generated. In addition, support for LINQ-compatible DataSet and DateTimeOffset is included.

What is the difference between event-based asynchronous models and callback / delegates?

EDIT: Is one way newer / better? I only get BeginXXX and EndXXX methods when I do not use the / tcv: Version35 switch. Silverlight uses XXXAsync, which tells me that I should use event-based methods (XXXAsync) and use this switch.

+4
source share
1 answer

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 { // Other details elided... public string GetData(int value) { return base.Channel.GetData(value); } } 

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.

+2
source

All Articles