David W Gray
David W Gray

Reputation: 699

.net transient database context being disposed prematurely

I am moving an asp.net mvc5 application using EF6 to asp.net core MVC 3.0 using EF Core.

In my mvc5 application I have some administrative operation that modify the database and take a long time, so I use a pattern when I create a new DBContext that is not the one that is associated with the request context and then run the task in the background using Task.Run. This has been working fine for years.

In converting to .net core it was unclear how to create a new DBContext in the way that I was doing it in my old codebase. It seems like I should be able to create a Transient DBContext in these cases and all should be fine.

So I created a subclass of MyDbContext called MyTransientDbContex and in my Configure class I added this service:

            services.AddDbContext<MyTransientDbContex>(options =>
                options.UseSqlServer(
                    context.Configuration.GetConnectionString("MyContextConnection")),
                ServiceLifetime.Transient, ServiceLifetime.Transient);

In my controller I inject the context in the action that needs the transient service and spawn a thread to do something with it:

public ActionResult Update([FromServices] MyTransientContext context) {

    Task.Run(() => 
    {
        try {
            // Do some long running operation with context
        }
        Catch (Exception e) {
            // Report Exception
        }
        finally {
            context.Dispose();
        }
    }

    return RedirectToAction("Status");
}

I would not expect my transient context to be disposed until the finally block. But I am getting this exception when attempting to access the context on the background thread:

Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'MyTransientContext'.'

And indeed the _disposed flag is set to true on the context object.

I put a breakpoint on the constructer for MyTransientContext and "Made an Object ID" of the this pointer so that I could track the object. This transient object is being created and is the same one that is inject into my controller action. It's also the same object that I'm trying to reference when the exception is thrown.

I tried setting a data breakpoint on the _disposed member in order to get a callstack on when disposed is being set to true, but the breakpoint won't bind.

I also tried overriding the Dispose method on MyTransientContext, and it isn't called until my explicit dispose in the finally block, which is after the exception is thrown and caught.

I feel like I'm missing something fundamental here. Isn't this what the transient services are for? What would dispose a Transient service?

One last detail - MyTransientContext is derived from MyContext, which is in turn derived from IdentityDbContext (Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContex)

Edit: The reason that I went down the path of using a Transient was because of this ef core document page: https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext. It states that "...any code that explicitly executes multiple threads in parallel should ensure that DbContext instances aren't ever accessed concurrently. Using dependency injection, this can be achieved by either registering the context as scoped and creating scopes (using IServiceScopeFactory) for each thread, or by registering the DbContext as transient (using the overload of AddDbContext which takes a ServiceLifetime parameter)."

As xabikos pointed out, this seems to be overriden by the scoping of the asp.net DI system, where it looks like anything created by that system is scoped to the request context, including Transient objects. Can someone point out where that's documented so that I can better understand how to work with the limitations?

Upvotes: 1

Views: 1807

Answers (2)

vernou
vernou

Reputation: 7610

f you want manage the lifetime of service, you can instantiate it manually (or use a factory) :

public ActionResult Update()
{
    Task.Run(() => 
    {
        using(var context = new MyTransientContext(...))
        {
            try 
            {
                // Do some long running operation with context
            }
            catch (Exception e) 
            {
                // Report Exception
            }
        }
    }
    return RedirectToAction("Status");
}

Or you can use IServiceProvider to get and manage a service :

public class MyController
{
    private IServiceProvider _services;

    public MyController(IServiceProvider services)
    {
        _services = services;
    }

    public ActionResult Update()
    {
        var context = (MyTransientContext)_services.GetService(typeof(MyTransientContext));
        Task.Run(() =>
        {
            using (context)
            {
                try
                {
                    // Do some long running operation with context
                }
                catch (Exception e)
                {
                    // Report Exception
                }
            }
        }
        return RedirectToAction("Status");
    }
}

Upvotes: 1

xabikos
xabikos

Reputation: 195

You mixed the concepts of transient objects that are created by internal DI container asp.net core provides.

You configure the MyTransientContext to be transient in the internal DI system. This practically means that every time a scope is created then a new instance is returned. For asp.net application this scope matches an HTTP request. When the requests ends then all the objects are disposed if applicable.

Now in your code, that is a synchronous action method you spawn a Task with Task.Run. This is an async operation and you don't await for this. Practically during execution this will be started but not wait to finish, the redirect will happen and the request will end. At this point if you try to use the injected instance you will get the exception.

If you would like to solve this you need change to an async action and await on the Task.Run. And most likely you don't need to spawn a new Task. But you need to understand that this is not probably the best way as it will need for the long operation to finish before the redirect takes place.

An alternative to this would be to use a messaging mechanism, and send a message that triggers this operation. And you have another component, like worker service that listens for those messages and process them.

Upvotes: 0

Related Questions