Asynchronous web request, EntityFramework and DI, how does it work?

I searched a lot about these items, but I'm still not sure if it works as expected. And why.

What I understand:

  • When processing a web request, you use one of the IIS threads from your pool. As part of this request, if you use an asynchronous call, for example, to request some data from a database, you release this thread so that IIS can use the same thread to handle another call. It's good. Maybe.
  • Sometime later, when the database finally produces the expected data, the code resumes. The asynchronous documentation mentions that you MAY now be in a different thread. Or not. DotNet Solution. If it is in the same stream as before, this is normal.
  • I use Injection Dependency to inject and close contexts throughout the life of PerRequest (using Microsoft Unity). Closing is my main concern. It works fine in my synchronous world: dbcontext is closed at the end of my web request.
  • EntityFramework is known to have a secure DbContext stream NOT

Question 1: If the renewal code is in a different thread, should IIS process all requests from one thread pool or is it from another side pool?

Question 2: If the code is running in a different thread, what about the WebRequest context? Will DI correctly track the end of this deferred call and not call Dispose () before the async code is really finished?

Question 3: If I use asynchronous EntityFramework methods, such as ToListAsync or FirstOrDefaultAsync, I read everywhere that "should be fine." Can someone clarify this? Does EF execute a specific web request or start stream? Is there some kind of capture? Will mixed dbcontext with another web request reuse my initial stream?

Question 4: If I use the usual (synchronous) EntityFramework methods, but wrapped in a Task. What will happen? Is it still "This should be good"?

Sorry, this is a lot of questions, it bothered me for a long time.

+5
source share
1 answer

Well, now I can answer my questions.

Question 1 : From Do asynchronous operations in ASP.NET MVC thread from ThreadPool to .NET 4

Yes - all threads from the pool thread. Your MVC application is already multithreaded when the request arrives in a new thread, will be taken from the pool and used to serve the request. This thread will be "blocked" (from other requests) until the request is fully serviced and completed. If there is no thread in the pool, the request will have to wait until it is available. If you have asynchronous controllers, they still receive the stream from the pool, but when serving the request, they can refuse the stream, waiting for something (and this stream can be transferred to another request), and when the original request needs the stream again, it gets one from the pool .

Question 2: If the code is running in a different thread, what about the WebRequest context?

From ewk and http://vegetarianprogrammer.blogspot.fr/2012/12/understanding-synchronizationcontext-in.html and https://msdn.microsoft.com/en-us/magazine/gg598924.aspx

In the SynchronisationContext, verify that the web request (HttpContext.Current) is correctly installed in the current thread when execution resumes. This is part of the background work of ASP.NET.

Will DI correctly track the completion of this deferred call and not call Dispose () before the async code actually ends?

Since the web request passes through as many threads as possible and does not end with the initial stream, I see no reason why the EndRequest event will fire before the full stream is completed. Thus, I no longer see the reasons why the PerRequestLifeTimeManager or other PerRequest strategy will fail by calling some Dispose () before it is needed.

Question 3 Γ  4 : Async and EF. As ewk mentioned, Entity Framework is safe if you do not use it in different threads at the same time. Since questions 3 and 4 show different ways of getting into DbContext, the answer is this: to be safe, there should be only one simultaneous access to DbContext, regardless of how you run tasks. As mentioned in ewk, direct rotation of two tasks using the same dbcontext is NOT safe.

0
source

Source: https://habr.com/ru/post/1273079/


All Articles