繁体   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