C# 获得;无法解析类型为“”的服务;升级到Core 2预览版2后
我刚刚升级到ASP.NET Core 2 Preview 2,在依赖性注入方面遇到了问题。我明白了 无法解析类型为的服务 参数“context”的“LC.Tools.API.Data.GenericDbContext” “LC.Tools.API.Startup”类型上的方法“Configure” 在运行项目时 我在使用旧版本时没有这个问题 DbContext(GenericDbContext):C# 获得;无法解析类型为“”的服务;升级到Core 2预览版2后,c#,.net,entity-framework,asp.net-core,asp.net-core-2.0,C#,.net,Entity Framework,Asp.net Core,Asp.net Core 2.0,我刚刚升级到ASP.NET Core 2 Preview 2,在依赖性注入方面遇到了问题。我明白了 无法解析类型为的服务 参数“context”的“LC.Tools.API.Data.GenericDbContext” “LC.Tools.API.Startup”类型上的方法“Configure” 在运行项目时 我在使用旧版本时没有这个问题 DbContext(GenericDbContext): namespace LC.Tools.API.Data { public class Ge
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");
.
.
.
.
.
.
}
}
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");
}
}
}
}
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(类型
服务类型,服务提供者(服务提供者)
Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型
服务类型)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
提供程序,类型(服务类型)
Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(对象
实例,IAApplicationBuilder)
您试图将上下文注入到
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);
}
公共IServiceProvider配置服务(IServiceCollection服务){
services.AddOptions();
services.AddDbContext(options=>options.UseSqlServer(this.ConnectionString));
services.AddMvc();
//构建中间服务提供者
var serviceProvider=services.BuildServiceProvider();
//返回提供程序
返回服务提供商;
}
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、,
iLogger工厂(loggerFactory,IServiceProvider服务提供商){
//…为简洁起见,删除了其他代码
var context=serviceProvider.GetService();
Data.Debug.Init.Initalize(context,env);
}
@Nkosi的回答让我走上了正确的道路,但实际上你不需要那么多步骤,至少在2.0版及更高版本中:
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);
}
public void配置服务(IServiceCollection服务){
services.AddOptions();
services.AddDbContext(options=>options.UseSqlServer(this.ConnectionString));
services.AddMvc();
}
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、,
iLogger工厂(loggerFactory,IServiceProvider服务提供商){
//…为简洁起见,删除了其他代码
var context=serviceProvider.GetService();
Data.Debug.Init.Initalize(context,env);
}
您不需要从
ConfigureServices
返回任何内容,也不需要在我正在运行的版本(2.0)中构建中间提供程序查看这是否有助于您尝试将上下文注入无法工作的Configure
方法。从Configure
方法中删除注入的上下文,而是注入服务提供商,并尝试在该方法中解析上下文。我还想知道自上次版本在asp.net core 1中运行以来,它发生了哪些更改。我只是想,定制实现不应该需要它,因为它工作得很好。正如链接的答案中所解释的,现在只有某些服务在启动时可用。您实际上可以在ConfigureServices
方法中完成一切,而不是注入Configure
中,但是这是您的选择。您不需要构建服务提供商。我不知道你为什么这么做。
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);
}