EF Data Context - Async/Await & Multithreading

.net, async-await, c#, entity-framework, multithreading

Solution

We have a stalemate situation here. `AspNetSynchronizationContext`, which is responsible for the threading model of an ASP.NET Web API execution environment, does not guarantee that asynchronous continuation after `await` will take place on the same thread. The whole idea of this is to make ASP.NET apps more scalable, so less threads from `ThreadPool` are blocked with pending synchronous operations.

However, the `DataContext` class (part of LINQ to SQL ) is not thread-safe, so it shouldn't be used where a thread switch may potentially occurr across `DataContext` API calls. A separate `using` construct per asynchronous call will not help, either:

var something;
using (var dataContext = new DataContext())
{
    something = await dataContext.someEntities.FirstOrDefaultAsync(e => e.Id == 1);
}

That's because `DataContext.Dispose` might be executed on a different thread from the one the object was originally created on, and this is not something `DataContext` would expect.

If you like to stick with the `DataContext` API, calling it synchronously appears to be the only feasible option. I'm not sure if that statement should be extended to the whole EF API, but I suppose any child objects created with `DataContext` API are probably not thread-safe, either. Thus, in ASP.NET their `using` scope should be limited to that of between two adjacent `await` calls.

It might be tempting to offload a bunch of synchronous `DataContext` calls to a separate thread with `await Task.Run(() => { /* do DataContext stuff here */ })`. However, that'd be a known anti-pattern, especially in the context of ASP.NET where it might only hurt performance and scalability, as it would not reduce the number of threads required to fulfill the request.

Unfortunately, while the asynchronous architecture of ASP.NET is great, it remains being incompatible with some established APIs and patterns (e.g., here is a similar case). That's especially sad, because we're not dealing with concurrent API access here, i.e. no more than one thread is trying to access a `DataContext` object at the same time.

Hopefully, Microsoft will address that in the future versions of the Framework.

[UPDATE] On a large scale though, it might be possible to offload the EF logic to a separate process (run as a WCF service) which would provide a thread-safe async API to the ASP.NET client logic. Such process can be orchestrated with a custom synchronization context as an event machine, similar to Node.js. It may even run a pool of Node.js-like apartments, each apartment maintaining the thread affinity for EF objects. That would allow to still benefit from the async EF API.

[UPDATE] Here is some attempt to find a solution to this problem.

Problem

I frequently use async/await to ensure ASP.NET MVC Web API threads are not blocked by longer-running I/O and network operations, specifically database calls. The System.Data.Entity namespace provides a variety of helper extensions here, such as FirstOrDefaultAsync, ContainsAsync, CountAsync and so forth. However, since data contexts are not thread safe, this means that the following code is problematic: ``` var dbContext = new DbContext(); var something = await dbContext.someEntities.FirstOrDefaultAsync(e => e.Id == 1); var morething = await dbContext.someEntities.FirstOrDefaultAsync(e => e.Id == 2); ``` In fact, I'm sometimes seeing exceptions such as: System.InvalidOperationException: The connection was not closed. The connection's current state is open. Is the correct pattern then to use a separate `using(new DbContext...)` block for each asynchronous call to the database? Is it potentially more beneficial to just execute synchronous then instead?

Original source

Related problems