簡體   English   中英

如何讓 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 令牌。 我已經定義了我的自定義令牌事件處理程序類並將其綁定到 ASP.NET Core 機制,如下所示:

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,
        };
    });
}

這是我的類令牌事件:

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 中注冊。 如果我嘗試在IUserManager中傳入IUserManagerTokenAuthenticationEvents引發異常,即 ASP.NET Core 無法解析IUserManager 所以我的問題是

如何告訴 ASP.NET Core DI 從 Simple Injector 解析IUserManager .

就像 Simple Injector交叉連接ASP.NET Core 組件一樣,您可以反過來做同樣的事情,盡管您必須手動執行此操作。 例如:

services.AddScoped<IUserManager>(c => container.GetInstance<IUserManager>());

這個答案並沒有解決您最初的問題,但我確實注意到您將舊的 Simple Injector ASP.NET Core 配置模型與新的混合使用。 您目前擁有以下代碼:

    services.AddSimpleInjector(container, options =>
    {
        options
            .AddAspNetCore()
                .AddControllerActivation()
                .AddViewComponentActivation()
                .AddPageModelActivation()
                .AddTagHelperActivation();
        });

        services.EnableSimpleInjectorCrossWiring(container);
        services.UseSimpleInjectorAspNetRequestScoping(container);

但是,對EnableSimpleInjectorCrossWiringUseSimpleInjectorAspNetRequestScoping的調用是舊 API,將在未來版本中刪除。 當您調用.AddAspNetCore()時,這些功能會自動啟用。 因此,您可以將配置減少到以下內容:

    services.AddSimpleInjector(container, options =>
    {
        options
            .AddAspNetCore()
                .AddControllerActivation()
                .AddViewComponentActivation()
                .AddPageModelActivation()
                .AddTagHelperActivation();
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM