My desire is to create an Action MVC controller that performs lengthy I / O operations asynchronously. My goal is to avoid thread binding in the ASP.Net thread pool until this lengthy method completes.
The action calls two calls.
The first call is a third-party dll that does not contain asynchronous methods. This dll reads from a proprietary database and performs quite complex cpu-bound processing. This may take up to two seconds.
The second call uses the results of the first call as parameters that are passed to the database query using the Entity Framework.
Simplified, this action:
public async Task<ActionResult> MyActionAsync(arg1, arg2) { var parameters = 3rdPartyComponent.TakesLongTime(arg1, arg2); Task<List<MyClass>> genericList = null; using (DbContexts.MyDbContext db = new DbContexts.MyDbContext()) { genericList = await db.Database.SqlQuery<MyClass>(sql,parameters).ToListAsync(); } return View("MyView", genericList); }
I would like to call 3rdPartyComponent on hold. My initial idea was to do this:
var parameters = await Task.Run(() => 3rdPartyComponent.TakesLongTime()).ConfigurateAwait(false);
but I read several subject experts who categorically state that using Task.Run () inside asp.net MVC Action is counterproductive and should never be executed.
3rdPartyComponent is a black box of compiled code, it cannot be changed to add async methods.
Is there a way to make the 3rdPartyComponent call expected so that the whole action is performed without thread binding in the asp.net thread pool?
source share