我是 WCF 的新手。我正在提供一项需要计算冗长操作的服务。由于该方法很长,我想我可以通过返回一个任务来使操作异步。但它不起作用。我仍然收到超时异常。示例代码(不是我的实际代码)在下面展示了我的问题:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
Task<double> ComputePiAsync(ulong numDecimals);
}
internal class Calculator : ICalculator
{
public async Task<double> ComputePiAsync(ulong numDecimals)
{
return await SomeVeryVeryLongWayOfComputingPi(numDecimals);
}
}
// server
using (var host = new ServiceHost(typeof(Calculator), new Uri("net.pipe://localhost")))
{
host.AddServiceEndpoint(typeof(ICalculator), new NetNamedPipeBinding(), "Calculator");
host.Open();
Console.WriteLine("Service is running. Press <ENTER> to exit.");
Console.ReadLine();
host.Close();
}
// client
var factory = new ChannelFactory<ICalculator>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/Calculator"));
var calculator = factory.CreateChannel();
await calculator.ComputePiAsync(numDecimals); // <--- this call takes longer than 1 minute and I'm getting a timeout here.
那么我应该怎么做才能在我的服务上调用一个冗长的操作并异步等待结果呢?增加超时?如果我增加操作超时,让方法返回 Task 有什么意义?