简体   繁体   English

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

[英]Correct method of calling WCF service asynchronously

I have the WCF service which return the collection of objects. 我有WCF服务,它返回对象集合。

He is working code I started with (I'm not sure it is correct): 他正在编写我开始使用的代码(我不确定它是否正确):

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

I need to move this call to separate class: 我需要将此调用移到单独的类:

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;
    }
}

Compiler say me that async method must return Task (or Task<>). 编译器说我异步方法必须返回Task(或Task <>)。

How do I need to rewrite my method and how this method call must be if I want to be sure that my code will wait when the Web Service returns the data. 如果我想确保我的代码在Web Service返回数据时等待,我该如何重写我的方法以及此方法调用的方式。

Compiler say me that async method must return Task (or Task<>). 编译器说我异步方法必须返回Task(或Task <>)。

Then you make your async method return Task<> : 然后让你的async方法返回Task<>

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

Yes, this will cause your callers to use await , which means they must become async , etc., etc. async will "grow" through your code base like this; 是的,这将导致你的调用者使用await ,这意味着他们必须变得async ,等等。 async将通过你的代码库“增长”,就像这样; this "growth" is natural and should be embraced. 这种“增长”是自然的,应该被接受。

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

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