Benefits of returning "Task <int>" instead of "int" in WCF or WebAPI Method (IIS)
Consider the long synchronous "Foo" method:
public int Foo(int id)
{
// Do some expensive calculation
return 42;
}
And the WCF service hosted in IIS is called "FooService", which calls "Foo":
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class FooService
{
public Task<int> GetFoo(int id)
{
return Task.Factory.StartNew(() => return Foo(id));
}
public int GetFoo2(int id)
{
return Foo(id);
}
}
Do I get any benefits if I start and return the task for Foo? For example - reduce the load on the input-output stream?
+4
3 answers
WCF API, APM (BeginXX/EndXX). WCF? , , , , , .
, , , . , , . , , Task.Wait() Task<T>.Result.
:
- .
- IO (, , ..).
, WCF, WCF . StartNew, ( ):
public Task<int> GetFoo(int id)
{
return Task.Factory.StartNew(() => return Foo(id));
}
FromResult, :
public Task<int> GetFoo(int id)
{
return Task.FromResult(new Foo(id));
}
, , API , .
+3