繁体   English   中英

正确调用WCF服务的异步方法

[英]Correct method of calling WCF service asynchronously

我有WCF服务,它返回对象集合。

他正在编写我开始使用的代码(我不确定它是否正确):

List<AxaptaServiceReference.Inspection> remoteInspections = (await proxy.GetInspectionsByInspectorAsync(App._winlogin)).ToList<AxaptaServiceReference.Inspection>();

我需要将此调用移到单独的类:

public class AxGateWay
{
    public async List<AxaptaServiceReference.Inspection> GetInspections(string _inspector) 
    {
        AxaptaServiceReference.AxaptaWebServiceClient proxy = new AxaptaServiceReference.AxaptaWebServiceClient();

        List<AxaptaServiceReference.Inspection> remoteInspections = (await task).ToList<AxaptaServiceReference.Inspection>();
        return remoteInspections;
    }
}

编译器说我异步方法必须返回Task(或Task <>)。

如果我想确保我的代码在Web Service返回数据时等待,我该如何重写我的方法以及此方法调用的方式。

编译器说我异步方法必须返回Task(或Task <>)。

然后让你的async方法返回Task<>

public async Task<List<AxaptaServiceReference.Inspection>> GetInspectionsAsync(string _inspector) 
{
  List<AxaptaServiceReference.Inspection> remoteInspections = ...;
  return remoteInspections;
}

是的,这将导致你的调用者使用await ,这意味着他们必须变得async ,等等。 async将通过你的代码库“增长”,就像这样; 这种“增长”是自然的,应该被接受。

暂无
暂无

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

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