c# - Async/Await and multi-layer web application -


because of optimalization issues, decided rewrite controller's action method asynchronous one. multi-layer application, , since that, i'm facing architecture problem, , wonder main differences between 2 approches show below. let's synchronous method is:

public virtual actionresult method() {    somelogic.largeoperation();    return view(...); } 

largeopertation lot. here's pseudocode:

public void largeopertion() {    databasemanipulations1();    independentwork();    callingtowebservice1();    independentwork();        databasemanipulations2();    independentwork();    callingtowebservice2();    independentwork();  } 

each method inside largeoperations has couple methods inside itself, , on ... question is: need make them async , use await in every of application layer?

public virtual task<actionresult> method() {    await somelogic.largeoperation();    return view(...); }  public async task largeopertion() {    await databasemanipulations1();    independentwork();    await callingtowebservice1();    independentwork();        await databasemanipulations2();    independentwork();    await callingtowebservice2();    independentwork();  } 

or can use task on largeopertaion that:

public virtual task<actionresult> method() {    await task.run(() => somelogic.largeoperation());    return view(...); } 

let's assume independentwork() not large.

because of optimalization issues, decided rewrite controller's action method asynchronous one.

before start, should realize async and won't.

asynchronous operations not run faster. so, asynchronous call database not faster synchronous call database.

await doesn't return browser early. release current thread asp.net threadpool.

so, each individual request still takes same total amount of time (actually, longer, not detectable amount).

what async does scalability - is, ability of application handle more requests same resources. however, helps scalability of web app - can't magically reach database , make that scale. so, if scalability bottleneck database , not asp.net (which case), async won't @ all.

if database backend scalable (e.g., nosql, azure sql, or database cluster), , if asp.net app needs scale up, can benefit async.

do need make them async , use await in every of application layer?

the best approach start @ "leaves" - lowest-level methods - , work way there. in case, start @ database interaction , convert async first.

however, @ no point should use task.run or task.factory.startnew. instead, use true asynchronous apis, such async support in ef6.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -