【发布时间】:2018-10-13 18:10:24
【问题描述】:
假设我有一个 WCF Websocket 服务:
[ServiceContract(CallbackContract =typeof(ICallback))]
public interface ISomeInterface
{
[OperationContract]
string GiveMeString();
}
现在,在客户端,感谢 WCF,我还可以使用 GiveMeStrignAsync 方法。现在,我想创建使用这种 WCF 生成的异步方法的客户端 API。 有两种方法可以做到这一点:
public Task<string> GiveMeStringAsync() //method on client side
{
Task<string> task = null;
try
{
task = ServiceReference.GiveMeStringAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return task;
}
那我就等待这个方法。
或者也许在这个方法中使用 await:
public async Task<string> GiveMeStringAsync() //method on client side
{
string s = String.Empty;
try
{
s = await ServiceReference.GiveMeStringAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return s;
}
我的问题:
根据我对 async/await 的正确理解,在第二个示例中,我仍需要在某处等待此方法,因此会有更多等待。更多的等待会导致性能下降?
假设我是对的,选项 1 是可行的方法。如果在我的客户端我有一些该 API 的包装器(不管为什么)。在这个包装类中,当调用包装类的方法(调用API的方法)时,我仍然应该返回(而不是等待)任务并只使用一次等待?
【问题讨论】:
标签: c# wcf asynchronous async-await