8
I'm new to WCF. I'm making a service in which I need to compute a lengthy operation. Since the method is lengthy I thought I could make the operation async by returning a Task. But it does not work. I'm still getting a Timeout exception. Sample code (not my actual code) demonstrating my problem below:
[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.
So what should I do in order to call a lengthy operation on my service and wait for the result asynchronously ? Increase timeout ? If I increase the operation timeout, what's the point of having the method return a Task ?