[英]How to convert sync calls to Async call of WCF?
目前,我们有几个WCF服务。 它们被同时调用,如下所示:
XYZ.XYZClient test= new XYZ.XYZClient();
bool result = test.Add(1,2);
有人可以解释我如何将其转换为异步调用吗?
如果您遵循此MSDN文章 。
它将变成:
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);
操作的事件参数
[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]));
}
}
}
完成时执行的代码
// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
Console.WriteLine("Add Result: {0}", e.Result);
}
代表
public event System.EventHandler<AddCompletedEventArgs> AddCompleted;
更改您的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.