tags:

views:

1005

answers:

1

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

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

What is the difference between event-based and callback/delegate based async models?

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

+2  A: 

Let's define a WCF service like this:

namespace StackOverflow
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string GetName();
    }

    public class Test : ITest
    {
        public string GetName()
        {
            return "Joel Spolsky";
        }
    }
}

If you run svcutil on this, you'll 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'll 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);
    }
}

So the /async flag simply provides a means for you to interact with your service asynchronously instead of the default synchronous-only behavior.

The GetDataAsync() method invokes the GetData() method asynchronously and notifies you when it is complete via the GetDataCompleted event.

The BeginGetData() and EndGetData() methods use the asynchronous behavior of delegates to invoke the GetData() method asynchronously. This is analogous to the BeginInvoke() and EndInvoke() methods on the System.Windows.Forms.Control class or the BeginRead() and EndRead() methods on the System.IO.Stream class.

Matt Davis
pros/cons of each type?
Scott
The choice between synchronous and asynchronous interaction depends on what you're doing. If you're interacting with your service from a GUI, asynchronous would be the way to go in order to keep the GUI responsive to user input. Other than that, synchronous is the most straight-forward.
Matt Davis
As for choosing between GetDataAsync() and the paired BeginGetData()/EndGetData(), keep in mind that every call to BeginGetData() must be paired with a call to EndGetData(). Personally, I prefer the GetDataAsync() since I can just register with the event and handle it whenever it occurs.
Matt Davis