简体   繁体   English

如何将同步调用转换为WCF的异步调用?

[英]How to convert sync calls to Async call of WCF?

At present we have a couple of WCF service. 目前,我们有几个WCF服务。 They are called sysnchronously as follows, 它们被同时调用,如下所示:

XYZ.XYZClient test= new XYZ.XYZClient();
bool result = test.Add(1,2);

Can someone explain me how we can convert this onto a Async call? 有人可以解释我如何将其转换为异步调用吗?

If you follow this MSDN article . 如果您遵循此MSDN文章

It would become: 它将变成:

double value1 = 100.00D;
double value2 = 15.99D;
XYZ.XYZClient test= new XYZ.XYZClient();
test.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
test.AddAsync(1, 2);
Console.WriteLine("Add({0},{1})", value1, value2);

Event args for the operation 操作的事件参数

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class AddCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
{
    private object[] results;

    public AddCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
            base(exception, cancelled, userState)
    {       this.results = results;         }

    public double Result
    {
        get            {
            base.RaiseExceptionIfNecessary();
            return ((double)(this.results[0]));
        }
    }
}

Code that executes on completion 完成时执行的代码

// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
    Console.WriteLine("Add Result: {0}", e.Result);
}

Delegate 代表

public event System.EventHandler<AddCompletedEventArgs> AddCompleted;

Change your wcf-proxies to generate task-based operations and use async/await. 更改您的wcf-proxies以生成基于任务的操作并使用async / await。

XYZ.XYZClient test= new XYZ.XYZClient(); 
bool result = await test.AddAsync(1, 2);

服务参考设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM