Getting "Could not resolve service type .." after upgrading to Core 2 Preview 2

I just upgraded to ASP.NET Core 2 Preview 2 and ran into a problem with depedency injection. I get

Failed to resolve service of type 'LC.Tools.API.Data.GenericDbContext' for parameter 'context' of the Configure method to type 'LC.Tools.API.Startup' when starting the project.

I did not have this problem when using the old version.

DbContext (GenericDbContext):

namespace LC.Tools.API.Data { public class GenericDbContext : DbContext { public GenericDbContext(DbContextOptions<GenericDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { //Generic builder.Entity<Client>(); builder.Entity<Graphic>(); . . . . . //Shop builder.Entity<Models.Shop.Store>().ToTable("ShopClient"); builder.Entity<Models.Shop.Category>().ToTable("ShopCategory"); . . . . . . } } 

Startup.cs:

 namespace LC.Tools.API { public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); this.HostingEnvironment = env; } public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.AddDbContext<Data.GenericDbContext>(options => options.UseSqlServer(this.ConnectionString)); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, Data.GenericDbContext context) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor, ForwardLimit = 2 }); app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } app.UseStaticFiles(); app.UseMvc(); Data.Debug.Init.Initalize(context, env); } private IHostingEnvironment HostingEnvironment { get; set; } public IConfigurationRoot Configuration { get; } private string ConnectionString { get { return this.HostingEnvironment.IsDevelopment() ? Configuration.GetConnectionString("Development") : Configuration.GetConnectionString("Production"); } } } } 

An exception:

An error occurred while starting the application.

InvalidOperationException: It is not possible to allow work with the scope 'LC.Tools.API.Data.GenericDbContext' from the root provider.

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution (type serviceType, ServiceProvider serviceProvider) Exception: Failed to allow service of type "LC.Tools.API.Data.GenericDbContext" for the "context" parameter of the LC method. Tools.API.Startup '.

Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke (instance object, IApplicationBuilder builder)

 InvalidOperationException: Cannot resolve scoped service 'LC.Tools.API.Data.GenericDbContext' from root provider. 

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution (type serviceType, ServiceProvider serviceProvider) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService (type Service Type) Microsoft.Extensions.DependencyInerionervice ServicePervicePervicePervicePervicePervicePervicePervicePervicePervicePervicePervicePervice AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke (instance object, IApplicationBuilder builder)

+2
source share
2 answers

You are trying to embed a context in a Configure method that will not work. Remove the entered context from the Configure method and instead add the service provider and try to resolve the context inside the method.

 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddOptions(); services.AddDbContext<Data.GenericDbContext>(options => options.UseSqlServer(this.ConnectionString)); services.AddMvc(); // Build the intermediate service provider var serviceProvider = services.BuildServiceProvider(); //return the provider return serviceProvider; } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { //...Other code removed for brevity var context = serviceProvider.GetService<Data.GenericDbContext>(); Data.Debug.Init.Initalize(context, env); } 
+3
source

@Nkosi's answer got me on the right track, but you really don't need many steps, at least in version 2.0 and above:

 public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.AddDbContext<Data.GenericDbContext>(options => options.UseSqlServer(this.ConnectionString)); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { //...Other code removed for brevity var context = serviceProvider.GetService<Data.GenericDbContext>(); Data.Debug.Init.Initalize(context, env); } 

You do not need to return anything from ConfigureServices or create an intermediate provider in the current version (2.0)

+1
source

All Articles