Ste*_*ler 3 .net c# entity-framework asp.net-core asp.net-core-2.0
我刚刚升级到 ASP.NET Core 2 Preview 2 并遇到了依赖注入的问题。我得到
运行项目时,无法解析类型为“LC.Tools.API.Startup”的方法“配置”的参数“上下文”的“LC.Tools.API.Data.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");
.
.
.
.
.
.
}
}
Run Code Online (Sandbox Code Playgroud)
启动.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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
启动应用程序时发生错误。
InvalidOperationException:无法从根提供程序解析范围服务“LC.Tools.API.Data.GenericDbContext”。
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider) 异常:无法为方法“Configure”的参数“context”解析“LC.Tools.API.Data.GenericDbContext”类型的服务输入“LC.Tools.API.Startup”。
Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(对象实例,IApplicationBuilder 构建器)
Run Code Online (Sandbox Code Playgroud)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 serviceType) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider)Microsoft. .Hosting.Internal.ConfigureBuilder.Invoke(对象实例,IApplicationBuilder 构建器)
您正在尝试将上下文注入到Configure
不起作用的方法中。从Configure
方法中删除注入的上下文,而是注入服务提供者并尝试解析方法中的上下文。
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8019 次 |
最近记录: |