C# 异步调用WCF服务的正确方法
我有返回对象集合的WCF服务 他正在使用我开始使用的代码(我不确定它是否正确):C# 异步调用WCF服务的正确方法,c#,wcf,asynchronous,async-await,windows-8.1,C#,Wcf,Asynchronous,Async Await,Windows 8.1,我有返回对象集合的WCF服务 他正在使用我开始使用的代码(我不确定它是否正确): List remoteInspections=(wait proxy.getinspectionsbyinspectionorasync(App.\u winlogin)).ToList(); 我需要将此调用移动到单独的类: public class AxGateWay { public async List<AxaptaServiceReference.Inspection> GetInsp
List remoteInspections=(wait proxy.getinspectionsbyinspectionorasync(App.\u winlogin)).ToList();
我需要将此调用移动到单独的类:
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;
}
}
公共类网关
{
公共异步列表GetInspections(字符串\u检查器)
{
AxaptaServiceReference.AxaptaWebServiceClient代理=新的AxaptaServiceReference.AxaptaWebServiceClient();
列出远程检查=(等待任务).ToList();
退货检查;
}
}
编译器告诉我异步方法必须返回Task(或Task)
如果我想确保我的代码在Web服务返回数据时会等待,那么我需要如何重写我的方法以及这个方法调用必须是怎样的
编译器告诉我异步方法必须返回Task(或Task)
然后使您的异步
方法返回任务
:
公共异步任务GetInspectionAsync(字符串\u检查器)
{
列出远程检查=。。。;
退货检查;
}
是的,这将导致调用者使用
wait
,这意味着他们必须成为async
,等等。async
将通过您的代码库像这样“增长”;这种“成长”是自然的,应该接受。你的例子中的“…”是什么?我需要在方法中使用wait吗?我刚刚展示了相关的要点。“…”是您已有的代码。
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;
}
}
public async Task<List<AxaptaServiceReference.Inspection>> GetInspectionsAsync(string _inspector)
{
List<AxaptaServiceReference.Inspection> remoteInspections = ...;
return remoteInspections;
}