" instead of "int" in WCF or WebAPI Method (IIS) Consider the long synchronous "Foo" method: public int Foo(...">

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
source share
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

-, , , , . , - WCF, WSHttpBinding BasicHTTPBinding ( ), , . , 10 , 12: 00: 01: 00, WCF .

, , , , , Task, .

+2

,

. , IO ( -/API, , ).

. , . .

, IIS 7,

... IIS 7.0, ASP.NET . , ( ). , , , , , .

: .net 4.5 Task.Run.

+1

All Articles