基于事件和基于回调/委托的异步方法之间的区别?

时间:2022-02-08 20:57:53

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

使用svcutil.exe时,我注意到了这个开关,/ tcv:Version35。文档说这个:

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.

版本35:如果要为使用.NET Framework 3.5的客户端生成代码,请使用/ tcv:Version35。通过使用此值,SvcUtil.exe工具生成引用.NET Framework 3.5和先前版本中的功能的代码。将/ tcv:Version35与/ async开关一起使用时,将生成基于事件和基于回调/委托的异步方法。此外,还启用了对启用LINQ的DataSet和DateTimeOffset的支持。

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.

编辑:是一种更新/更好的方式?当我不使用/ tcv:Version35开关时,我只获得BeginXXX和EndXXX方法。 Silverlight使用XXXAsync,它告诉我应该使用基于事件的(XXXAsync)方法并使用此开关。

1 个解决方案

#1


Let's define a WCF service like this:

让我们像这样定义一个WCF服务:

namespace *
{
    [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:

如果您对此运行svcutil,您将获得以下客户端定义:

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:

如果使用/ async标志再次运行svcutil,您将获得以下客户端定义:

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.

因此/ async标志只是为您提供了一种异步交互服务的方法,而不是默认的仅同步行为。

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

GetDataAsync()方法异步调用GetData()方法,并在通过GetDataCompleted事件完成时通知您。

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.

BeginGetData()和EndGetData()方法使用委托的异步行为来异步调用GetData()方法。这类似于System.Windows.Forms.Control类上的BeginInvoke()和EndInvoke()方法,或System.IO.Stream类上的BeginRead()和EndRead()方法。

#1


Let's define a WCF service like this:

让我们像这样定义一个WCF服务:

namespace *
{
    [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:

如果您对此运行svcutil,您将获得以下客户端定义:

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:

如果使用/ async标志再次运行svcutil,您将获得以下客户端定义:

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.

因此/ async标志只是为您提供了一种异步交互服务的方法,而不是默认的仅同步行为。

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

GetDataAsync()方法异步调用GetData()方法,并在通过GetDataCompleted事件完成时通知您。

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.

BeginGetData()和EndGetData()方法使用委托的异步行为来异步调用GetData()方法。这类似于System.Windows.Forms.Control类上的BeginInvoke()和EndInvoke()方法,或System.IO.Stream类上的BeginRead()和EndRead()方法。