【问题标题】:Correct method of calling WCF service asynchronously异步调用WCF服务的正确方法
【发布时间】:2014-09-16 14:24:50
【问题描述】:

我有返回对象集合的 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;
    }
}

编译器告诉我异步方法必须返回任务(或任务)。

如果我想确保我的代码在 Web 服务返回数据时等待,我需要如何重写我的方法以及该方法调用必须如何。

【问题讨论】:

    标签: c# wcf asynchronous async-await windows-8.1


    【解决方案1】:

    编译器告诉我异步方法必须返回任务(或任务)。

    然后你让你的async方法返回Task&lt;&gt;:

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

    是的,这将导致您的调用者使用await,这意味着他们必须成为async,等等等等。async 将像这样在您的代码库中“增长”;这种“增长”是自然的,应该被接受。

    【讨论】:

    • 您的示例中的“...”是什么?我需要在方法中使用 await 吗?
    • 我只是展示了相关点。 “...”是您已经拥有的代码。
    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2011-05-04
    • 2012-10-03
    相关资源
    最近更新 更多