如何让 ASP.NET Core DI 来让 Simple Injector 解决依赖?
How to have ASP.NET Core DI to get the dependency resolved by Simple Injector?
在一个 ASP.NET Core web api 项目中,我使用了 Simple Injector 和 JwtBearer 令牌。我已经定义了我的自定义令牌事件处理程序 class 并将其绑定到 ASP.NET 核心机制,如下所示:
private void ConfigureSecurity(IServiceCollection services)
{
var encodedKey = Encoding.UTF8.GetBytes(_config[Konstants.SecretKey]);
var key = new SymmetricSecurityKey(encodedKey);
services.AddTransient<TokenAuthenticationEvents>();
var authentication = services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
});
authentication.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.SaveToken = true;
options.EventsType = typeof(TokenAuthenticationEvents);
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = _config[Konstants.AudienceKey],
ValidateIssuer = true,
ValidIssuer = _config[Konstants.AuthorityKey],
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
RequireExpirationTime = true,
ValidateLifetime = true,
};
});
}
这是我的 class 令牌事件:
public class TokenAuthenticationEvents : JwtBearerEvents
{
private readonly ILogger _log;
private readonly IUserManager _users;
public TokenAuthenticationEvents(ILogger log)
{
_log = log ?? throw new ArgumentNullException(nameof(log));
_users = null;
}
public override Task TokenValidated(TokenValidatedContext context)
{
//Some Custom Logic that requires IUserManager
return base.TokenValidated(context);
}
}
我正在使用 Simple Injector 作为我的 DI 容器
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
IUserManager
及其所有依赖项都已在 Simple Injector 中注册。如果我尝试在 TokenAuthenticationEvents
中传递 IUserManager
,则会引发异常,即 ASP.NET Core 无法解析 IUserManager
。所以我的问题是
我如何告诉 ASP.NET Core DI 从 Simple Injector 中解析 IUserManager
?.
就像 Simple Injector cross wires ASP.NET Core components,你可以反过来做同样的事情,尽管你必须手动做。例如:
services.AddScoped<IUserManager>(c => container.GetInstance<IUserManager>());
这个答案没有解决您最初的问题,但我确实注意到您将旧的简单注入器 ASP.NET 核心配置模型与新的混合在一起。您目前拥有以下代码:
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
然而,对 EnableSimpleInjectorCrossWiring
和 UseSimpleInjectorAspNetRequestScoping
的调用是旧的 API,将在未来的版本中删除。当您调用 .AddAspNetCore()
时,这些功能会自动启用。所以你可以减少你的配置如下:
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});
在一个 ASP.NET Core web api 项目中,我使用了 Simple Injector 和 JwtBearer 令牌。我已经定义了我的自定义令牌事件处理程序 class 并将其绑定到 ASP.NET 核心机制,如下所示:
private void ConfigureSecurity(IServiceCollection services)
{
var encodedKey = Encoding.UTF8.GetBytes(_config[Konstants.SecretKey]);
var key = new SymmetricSecurityKey(encodedKey);
services.AddTransient<TokenAuthenticationEvents>();
var authentication = services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
});
authentication.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.SaveToken = true;
options.EventsType = typeof(TokenAuthenticationEvents);
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = _config[Konstants.AudienceKey],
ValidateIssuer = true,
ValidIssuer = _config[Konstants.AuthorityKey],
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
RequireExpirationTime = true,
ValidateLifetime = true,
};
});
}
这是我的 class 令牌事件:
public class TokenAuthenticationEvents : JwtBearerEvents
{
private readonly ILogger _log;
private readonly IUserManager _users;
public TokenAuthenticationEvents(ILogger log)
{
_log = log ?? throw new ArgumentNullException(nameof(log));
_users = null;
}
public override Task TokenValidated(TokenValidatedContext context)
{
//Some Custom Logic that requires IUserManager
return base.TokenValidated(context);
}
}
我正在使用 Simple Injector 作为我的 DI 容器
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
IUserManager
及其所有依赖项都已在 Simple Injector 中注册。如果我尝试在 TokenAuthenticationEvents
中传递 IUserManager
,则会引发异常,即 ASP.NET Core 无法解析 IUserManager
。所以我的问题是
我如何告诉 ASP.NET Core DI 从 Simple Injector 中解析 IUserManager
?.
就像 Simple Injector cross wires ASP.NET Core components,你可以反过来做同样的事情,尽管你必须手动做。例如:
services.AddScoped<IUserManager>(c => container.GetInstance<IUserManager>());
这个答案没有解决您最初的问题,但我确实注意到您将旧的简单注入器 ASP.NET 核心配置模型与新的混合在一起。您目前拥有以下代码:
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
然而,对 EnableSimpleInjectorCrossWiring
和 UseSimpleInjectorAspNetRequestScoping
的调用是旧的 API,将在未来的版本中删除。当您调用 .AddAspNetCore()
时,这些功能会自动启用。所以你可以减少你的配置如下:
services.AddSimpleInjector(container, options =>
{
options
.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation()
.AddPageModelActivation()
.AddTagHelperActivation();
});