简体   繁体   English

AspNetCoreRateLimit .NET Core 3.0 - 无法解析参数 IMemoryCache 缓存

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

Since switching to .NET Core 3.0 and 3.1 I've been getting the following error with AspNetCoreRateLimit when the application/API starts up:自从切换到 .NET Core 3.0 和 3.1 后,当应用程序/API 启动时,AspNetCoreRateLimit 出现以下错误:

'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'AspNetCoreRateLimit.MemoryCacheRateLimitCounterStore' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.Extensions.Caching.Memory.IMemoryCache cache' of constructor 'Void .ctor(Microsoft.Extensions.Caching.Memory.IMemoryCache)'.可以使用可用服务和参数调用类型“AspNetCoreRateLimit.MemoryCacheRateLimitCounterStore”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”:无法解析构造函数“Void .ctor( Microsoft.Extensions.Caching.Memory.IMemoryCache)'。

My service configuration is like this:我的服务配置是这样的:

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"];
        });

I understand the error is saying it can't resolve the dependency IMemoryCache and by adding the following to the startup I'm able to get rid of it:我知道错误是说它无法解决依赖项 IMemoryCache 并且通过将以下内容添加到启动中我可以摆脱它:

services.AddSingleton<IMemoryCache, MemoryCache>()

But what concerns me is this didn't happen in earlier versions of .NET Core, it's not in any of the AspNetCoreRateLimit docs and I don't really know understand the implications of simply adding injecting the MemoryCache are!但让我担心的是,这在 .NET Core 的早期版本中没有发生,它不在任何 AspNetCoreRateLimit 文档中,我真的不知道简单地添加注入 MemoryCache 的含义是什么!

Can anyone help me figure out what I'm missing/am doing wrong and why this has started happening in the new versions of .NET Core but just works in .NET Core 2.1?谁能帮我弄清楚我遗漏了什么/做错了什么,以及为什么这在 .NET Core 的新版本中开始发生,但只适用于 .NET Core 2.1?

You are adding an IRateLimitCounterStore to the pipeline here:您正在IRateLimitCounterStore向管道添加IRateLimitCounterStore

.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>()

You can see from the source that the MemoryCacheRateLimitCounterStore class takes an IMemoryCache in its constructor:源码可以看出, MemoryCacheRateLimitCounterStore类在其构造函数中采用了一个IMemoryCache

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

If you don't provide an IMemoryCache to your pipeline, this class can't be constructed through DI (that's what the error is telling you).如果您不向管道提供IMemoryCache ,则无法通过 DI 构造此类(这就是错误告诉您的内容)。

Looking at the history of the source file, it appears to have always required that parameter to its constructor.查看源文件的历史记录,它似乎总是需要在其构造函数中使用该参数。 Perhaps, in version 2.1, some other service was adding an IMemoryCache behind the scenes but is no longer adding one for you in 3.0.也许,在 2.1 版中,某些其他服务在幕后添加了IMemoryCache ,但在 3.0 中不再为您添加。

There is no real concern by adding a memory cache - it was always being added somehow as long as you've been using MemoryCacheRateLimitCounterStore .添加内存缓存并没有真正的问题 - 只要您一直在使用MemoryCacheRateLimitCounterStore它就会以某种方式添加。 Seems you just need to add it yourself at this point.看来你现在只需要自己添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 内核将带 IMemoryCache 的内存缓存转换为 Redis - ASP.NET Core Converting In-Memory Cache with IMemoryCache to Redis 如果我改变了从 Net Core IMemoryCache 获得的对象,它会更新它在缓存中的状态吗? - If I mutate the object obtained from Net Core IMemoryCache, will it update its state in the cache? 无法在 Dotnet Core 3.0 中使用 Redis 缓存 - Cannot use Redis cache in Dotnet Core 3.0 .net core 3.0 构造函数参数问题 - .net core 3.0 Constructor parameter problem 将DBContext放入IMemoryCache(.NET Core / EF Core)之后为什么要处理它 - Why is DBContext is disposed after putting it in IMemoryCache (.NET Core / EF Core) 无法解决 net.core 3.0 中的依赖 HttpClient - Unable to resolve dependency HttpClient in net.core 3.0 为什么在ASP.Net Core中获取IMemoryCache的多个实例? - Why getting multiple instances of IMemoryCache in ASP.Net Core? 在 ASP.net 核心应用程序中使用 IMemoryCache 和 Unity DI Container - Use IMemoryCache with Unity DI Container in ASP.net core application IMemoryCache保证独特的新密钥.NET-Core - IMemoryCache Guaranteed Unique new Keys .NET-Core .NET Core GetOrCreate 函数中的 IMemoryCache 被多次调用 - IMemoryCache in .NET Core GetOrCreate function is called multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM