WCF ServiceContract's method returning Task<T> and timeout -
i'm new wcf. i'm making service in need compute lengthy operation. since method lengthy thought make operation async returning task. not work. i'm still getting timeout exception. sample code (not actual code) demonstrating 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 running. press <enter> 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); // <--- call takes longer 1 minute , i'm getting timeout here.
so should in order call lengthy operation on service , wait result asynchronously ? increase timeout ? if increase operation timeout, what's point of having method return task ?
since method lengthy thought make operation async
hold on, slow down - mixing asynchrony network operation timeout.
lets consider asynchrony first:
1. using async/await
asynchronously call wcf service client
async/await
on client side helps write asynchronous program application remains responsive - in case of wpf/winform
don't want freeze ui calling wcf service synchronously on ui thread. instead can call asynchronously using async/await
. magic here complete client side functionality - meaning service need not asynchronous or need not return task
- have to while creating service reference(proxy)
have tell visual studio
generate task
based async operations - , can use async/await
on client.
2. need async/await
on service side?
the service may perform i/o
query data source or in-turn call service. using async/await
on service side - service can perform these operations asynchronously - without blocking service threads. means service has more free threads serve new clients - hence service scales better.
now, above 2 should not confused long running service operation
when service takes long results, dealing network timeouts. cannot , should not try keep network connection indefinitely open without activity. wcf
helps configuring various timeouts. if service method takes long respond, have increase timeouts. can consider using wcf callback contracts
if want report progress client. if not suffice requirement re-design service such client can initiate long running process , call method on service query status.
Comments
Post a Comment