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