簡體   English   中英

AspNetCoreRateLimit .NET Core 3.0 - 無法解析參數 IMemoryCache 緩存

[英]AspNetCoreRateLimit .NET Core 3.0 - Cannot resolve parameter IMemoryCache cache

自從切換到 .NET Core 3.0 和 3.1 后,當應用程序/API 啟動時,AspNetCoreRateLimit 出現以下錯誤:

可以使用可用服務和參數調用類型“AspNetCoreRateLimit.MemoryCacheRateLimitCounterStore”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”:無法解析構造函數“Void .ctor( Microsoft.Extensions.Caching.Memory.IMemoryCache)'。

我的服務配置是這樣的:

services.AddControllers();

        services.AddApiVersioning(options =>
        {
            options.ReportApiVersions = true;
            options.ApiVersionReader = new UrlSegmentApiVersionReader();
        })
        .AddVersionedApiExplorer(options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.SubstituteApiVersionInUrl = true;
        })

        // Register the Swagger generation with the default options
        .AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>()
        .AddSwaggerGen(options =>
        {
            options.OperationFilter<SwaggerDefaultValues>();
            options.CustomSchemaIds(x => x.FullName);
        });

        services.AddCors();

        //add API throttling configuration
        services.Configure<CView.Core.Web.Settings.IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"))
            .AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>()
            .AddSingleton<IMemoryCache, MemoryCache>()
            .AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>()
            .AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>()
            .AddResponseCompression()
            .Configure<ExceptionHandlingOptions>(Configuration.GetSection("ExceptionHandlingOptions"))
            .Configure<ApiBehaviorOptions>(opt => { opt.SuppressModelStateInvalidFilter = true; })
            .Configure<RabbitMqMessageBus>(GetRabbitMqConfigurationSection())
            .AddMassTransit(x =>
            {
                x.AddBus(ConfigureRabbitMq);
                x.AddConsumer<CompanyNameUpdatedConsumer>();
            });

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddSingleton<IHostedService, RabbitMqHostedService>();
        services.AddAutoMapper(Assembly.GetAssembly(typeof(AutoMapperModule))); //If you have other mapping profiles defined, that profiles will be loaded too.
        services.Configure<Auth0Options>(Configuration.GetSection("Auth0"));

        var auth0Domain = $"{Configuration["Auth0:Domain"]}";

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.Authority = auth0Domain;
            options.Audience = Configuration["Auth0:Audience"];
        });

我知道錯誤是說它無法解決依賴項 IMemoryCache 並且通過將以下內容添加到啟動中我可以擺脫它:

services.AddSingleton<IMemoryCache, MemoryCache>()

但讓我擔心的是,這在 .NET Core 的早期版本中沒有發生,它不在任何 AspNetCoreRateLimit 文檔中,我真的不知道簡單地添加注入 MemoryCache 的含義是什么!

誰能幫我弄清楚我遺漏了什么/做錯了什么,以及為什么這在 .NET Core 的新版本中開始發生,但只適用於 .NET Core 2.1?

您正在IRateLimitCounterStore向管道添加IRateLimitCounterStore

.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>()

源碼可以看出, MemoryCacheRateLimitCounterStore類在其構造函數中采用了一個IMemoryCache

public MemoryCacheRateLimitCounterStore(IMemoryCache cache) : base(cache)
{
}

如果您不向管道提供IMemoryCache ,則無法通過 DI 構造此類(這就是錯誤告訴您的內容)。

查看源文件的歷史記錄,它似乎總是需要在其構造函數中使用該參數。 也許,在 2.1 版中,某些其他服務在幕后添加了IMemoryCache ,但在 3.0 中不再為您添加。

添加內存緩存並沒有真正的問題 - 只要您一直在使用MemoryCacheRateLimitCounterStore它就會以某種方式添加。 看來你現在只需要自己添加它。

暫無
暫無

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

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