Search code examples
c#asp.net-coreentity-framework-core

Cannot access a disposed object in ASP.NET Core when injecting DbContext


On an ASP.NET Core project I have the following on Startup:

  services.AddDbContext<Context>(x => x.UseSqlServer(connectionString));

  services.AddTransient<IValidationService, ValidationService>();

  services.AddTransient<IValidator<Model>, ModelValidator>();

The ValidationService is as follows:

public interface IValidationService {
    Task<List<Error>> ValidateAsync<T>(T model);
}

public class ValidationService : IValidationService {
    private readonly IServiceProvider _provider;

    public ValidationService(IServiceProvider provider) {
        _provider = provider;
    }

    public async Task<List<Error>> ValidateAsync<T>(T model) {
        IValidator<T> validator = _provider.GetRequiredService<IValidator<T>>();

        return await validator.ValidateAsync(model);
    }
}

And the ModelValidator is as follows:

public class ModelValidator : AbstractValidator<Model> {
  public ModelValidator(Context context) {
    // Some code using context
  }
}

When I inject a IValidationService in a controller and use it as:

List<Error> errors = await _validator.ValidateAsync(order);    

I get the error:

System.ObjectDisposedException: 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 is 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: 'Context'.

Any idea why I am having this error when using Context inside ModelValidator.

How to fix this?

UPDATE

So I changed the code to:

services.AddScoped<IValidationService, ValidationService>();

services.AddScoped<IValidator<Model>, ModelValidator>();

But I get the same error ...

UPDATE - Seed Data Code inside Configure method on Startup

So on Configure method I have:

if (hostingEnvironment.IsDevelopment())
  applicationBuilder.SeedData();

And the SeedData extension is:

public static class DataSeedExtensions {
    private static IServiceProvider _provider;

    public static void SeedData(this IApplicationBuilder builder) { 
        _provider = builder.ApplicationServices;
        _type = type;

        using (Context context = (Context)_provider.GetService<Context>()) {
            await context.Database.MigrateAsync();
            // Insert data code
    }
}

What am I missing?

UPDATE - A possible solution

Changing my Seed method to the following seems to work:

using (IServiceScope scope = 
    _provider.GetRequiredService<IServiceScopeFactory>().CreateScope()) {
    Context context = _provider.GetService<Context>();
    // Insert data in database
}

Solution

  • Update for ASP.NET Core 2.1

    In ASP.NET Core 2.1 the methods changed slightly. The general method is similar to the 2.0, just the methods name and return types have been changed.

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args)
            .Build()
            .Seed();
    }
    
    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return new WebHostBuilder()
            ...; // Do not call .Build() here
    }
    

    Applies for ASP.NET Core 2.0

    With ASP.NET Core 2.0 there have been some changes in how EF Core tools (dotnet ef migrations etc.) determine the DbContext and connection string at design time.

    The below answer leads that the migrations and seeding are applied when calling any of the dotnet ef xxx commands.

    The new pattern for getting a design time instance for the EF Core tools is by using an BuildHostWeb static method.

    As per this announcement, EF Core will now use the static BuildWebHost method which configures the whole application, but doesn't run it.

      public class Program
      {
          public static void Main(string[] args)
          {
              var host = BuildWebHost(args);
    
              host.Run();
          }
    
          // Tools will use this to get application services
          public static IWebHost BuildWebHost(string[] args) =>
              new WebHostBuilder()
                  .UseKestrel()
                  .UseContentRoot(Directory.GetCurrentDirectory())
                  .UseIISIntegration()
                  .UseStartup<Startup>()
                  .Build();
      }
    

    Replace this in your old Main method

    public static void Main(string[] args)
    {
        var host = BuildWebHost(args)
            .Seed();
    
        host.Run();
    }
    

    Where Seed is an extension method:

    public static IWebHost Seed(this IWebHost webhost)
    {
        using (var scope = webhost.Services.GetService<IServiceScopeFactory>().CreateScope())
        {
            // alternatively resolve UserManager instead and pass that if only think you want to seed are the users     
            using (var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>()) 
            {
                SeedData.SeedAsync(dbContext).GetAwaiter().GetResult();
            }
        }
    }
    
    public static class SeedData
    {
        public static async Task SeedAsync(ApplicationDbContext dbContext)
        {
            dbContext.Users.Add(new User { Id = 1, Username = "admin", PasswordHash = ... });
        }
    }
    

    Old Answer, still applies to ASP.NET Core 1.x

    There is a semi-official pattern on how to seed Entity Framework Core in ASP.NET Core application you should apply, because during application startup there is no Request and hence no RequestServices (which resolves scoped services).

    In essence it boils down to creating a new scope, resolve the types you need and dispose the scope again once you're finished.

    // serviceProvider is app.ApplicationServices from Configure(IApplicationBuilder app) method
    using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
    {
        var db = serviceScope.ServiceProvider.GetService<AppDbContext>();
    
        if (await db.Database.EnsureCreatedAsync())
        {
            await SeedDatabase(db);
        }
    }
    

    One of the reasons directly resolving a service via app.ApplicationServices.GetService<MyService>() is that ApplicationServices is the application (or lifetime) scope provider and the services resolved here stay alive until the application is shut down.

    Usually the scoped container will resolve from it's parent container, if the object already exists there. So if you instantiate the DbContext this way in the application, it will be available in ApplicationServices container and when a request happens, a child container will be created.

    Now when resolving the DbContext it won't be resolved as scoped, because it already exists in the parent container, so the instance of the parent container will be returned instead. But since it has been disposed during the seeding, it won't be accessible.

    A scope container is nothing else then a singleton container with limited lifetime.

    So never resolve scoped services in Application startup w/o using the pattern above of first creating a scope and resolving from it.