问题描述
当我尝试在 Startup.cs
文件的 Configure
方法中使用自定义DbContext时,收到以下异常。我在2.0.0-preview1-005977版本中使用ASP.NET Core
I receive following exception when I try to use custom DbContext in Configure
method in Startup.cs
file. I use ASP.NET Core in version 2.0.0-preview1-005977
此异常
这是我的 ConfigureServices
和 Configure
方法。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CommunicatorContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddCookieAuthentication();
services.Configure<LdapConfig>(Configuration.GetSection("Ldap"));
services.AddScoped<ILdapService, LdapService>();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CommunicatorContext dbContext, ILdapService ldapService)
{
app.UseAuthentication();
app.UseWebSockets();
app.Use(async (context, next) =>
{
if (context.Request.Path == "/ws")
{
if (context.WebSockets.IsWebSocketRequest)
{
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(context, webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
app.UseMvc();
DbInitializer.Initialize(dbContext, ldapService);
}
推荐答案
报价文档
ASP.NET Core依赖项注入在$期间提供应用程序服务b $ b应用程序的启动。您可以通过在$$$$$构造函数或其中的配置之一中将
类中,以请求这些服务。 或
作为适当的接口作为参数添加到$$$$ Start $ ConfigureServices
方法。
ASP.NET Core dependency injection provides application services during an application's startup. You can request these services by including the appropriate interface as a parameter on your Startup
class's constructor or one of its Configure
or ConfigureServices
methods.
查看 Startup 类,按照它们调用
的顺序,可能需要以下服务作为
参数:
Looking at each method in the Startup
class in the order in which they are called, the following services may be requested as parameters:
- 在构造函数中:
IHostingEnvironment
,ILoggerFactory
- 在
ConfigureServices
方法中:IServiceCollection
- 在<$ c中$ c>配置方法:
IApplicationBuilder
,IHostingEnvironment
,
ILoggerFactory
,IApplicationLifetime
- In the constructor:
IHostingEnvironment
,ILoggerFactory
- In the
ConfigureServices
method:IServiceCollection
- In the
Configure
method:IApplicationBuilder
,IHostingEnvironment
,ILoggerFactory
,IApplicationLifetime
您正在尝试解决启动期间不可用的服务,
You are trying to resolve services that are not available during startup,
...CommunicatorContext dbContext, ILdapService ldapService) {
这将为您提供错误信息。如果需要访问实现,则需要执行以下操作之一:
which will give you the errors you are getting. If you need access to the implementations then you need to do one of the following:
-
修改
ConfigureServices
方法,然后从服务集合中访问它们。即
Modify the
ConfigureServices
method and access them there from the service collection. i.e.
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddDbContext<CommunicatorContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddCookieAuthentication();
services.Configure<LdapConfig>(Configuration.GetSection("Ldap"));
services.AddScoped<ILdapService, LdapService>();
services.AddMvc();
// Build the intermediate service provider
var serviceProvider = services.BuildServiceProvider();
//resolve implementations
var dbContext = serviceProvider.GetService<CommunicatorContext>();
var ldapService = serviceProvider.GetService<ILdapService>();
DbInitializer.Initialize(dbContext, ldapService);
//return the provider
return serviceProvider();
}
修改 ConfigureServices
方法返回IServiceProvider,配置
方法获取 IServiceProvider
,然后在那里解析您的依赖项。即
Modify the ConfigureServices
method to return IServiceProvider, Configure
method to take a IServiceProvider
and then resolve your dependencies there. i.e.
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddDbContext<CommunicatorContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddCookieAuthentication();
services.Configure<LdapConfig>(Configuration.GetSection("Ldap"));
services.AddScoped<ILdapService, LdapService>();
services.AddMvc();
// Build the intermediate service provider then return it
return services.BuildServiceProvider();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, IServiceProvider serviceProvider) {
//...Other code removed for brevity
app.UseMvc();
//resolve dependencies
var dbContext = serviceProvider.GetService<CommunicatorContext>();
var ldapService = serviceProvider.GetService<ILdapService>();
DbInitializer.Initialize(dbContext, ldapService);
}
这篇关于ASP.NET Core 2中的依赖注入会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!