Nicht auf einen entsorgt Objekt in ASP.NET Kern beim Einspritzen von DbContext

Auf eine ASP.NET Kern-Projekt habe ich Folgendes beim Start:

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

  services.AddTransient<IValidationService, ValidationService>();

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

Den ValidationService ist wie folgt:

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);

}

}

Und die ModelValidator ist wie folgt:

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

Wenn ich Spritzen ein IValidationService in einem controller und verwenden Sie es als:

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

Bekomme ich die Fehlermeldung:

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'.    

Jede Idee, warum bin ich mit diesem Fehler bei der Verwendung von Kontext innerhalb ModelValidator.

Wie man dieses Problem beheben?

UPDATE

Also änderte ich den code, um:

services.AddScoped<IValidationService, ValidationService>();

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

Aber ich bekomme die gleiche Fehlermeldung ...

UPDATE - Seed-Daten-Code, der in Configure Methode auf Start

Also auf "Configure" - Methode habe ich noch:

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

Und die SeedData Erweiterung:

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

  }

}

Was bin ich?

UPDATE - EINE mögliche Lösung

Ändert sich meine Seed-Methode, die der folgenden scheint zu funktionieren:

using (IServiceScope scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope()) {

  Context context = _provider.GetService<Context>();

  //Insert data in database

}

InformationsquelleAutor der Frage Miguel Moura | 2016-08-01

Schreibe einen Kommentar