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

C# AspNetCoreRateLimit.NET Core 3.0-无法解析参数IMemoryCache缓存,c#,asp.net-core,autofac,rate-limiting,C#,Asp.net Core,Autofac,Rate Limiting,自从切换到.NET Core 3.0和3.1以来,当应用程序/API启动时,我发现AspNetCoreRateLimit出现以下错误: “AspNetCoreRateLimit.MemoryCacheRateLimitCounterStore”类型上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”可以使用可用的服务和参数调用: 无法解析构造函数Void.ctor(Microsoft.Extensions.Caching.

自从切换到.NET Core 3.0和3.1以来,当应用程序/API启动时,我发现AspNetCoreRateLimit出现以下错误:

“AspNetCoreRateLimit.MemoryCacheRateLimitCounterStore”类型上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”可以使用可用的服务和参数调用: 无法解析构造函数Void.ctor(Microsoft.Extensions.Caching.Memory.IMemoryCache)的参数“Microsoft.Extensions.Caching.Memory.IMemoryCache cache”

我的服务配置如下:

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"];
        });
services.AddControllers();
services.addapVersioning(选项=>
{
options.ReportApiVersions=true;
options.ApiVersionReader=new urlsectionapiversionreader();
})
.AddVersionedApiExplorer(选项=>
{
options.GroupNameFormat=“'v'VVV”;
options.substituteApprovisionInUrl=true;
})
//使用默认选项注册招摇过市生成
.AddTransient()
.AddSwaggerGen(选项=>
{
options.OperationFilter();
options.customSchemaAIDS(x=>x.FullName);
});
services.AddCors();
//添加API节流配置
services.Configure(Configuration.GetSection(“IpRateLimiting”))
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddResponseCompression()
.Configure(Configuration.GetSection(“异常处理选项”))
.Configure(opt=>{opt.SuppressModelStateInvalidFilter=true;})
.Configure(GetRabbitMqConfigurationSection())
.AddMassTransit(x=>
{
x、 AddBus(ConfigureRabbitMq);
x、 AddConsumer();
});
services.AddSingleton();
services.AddSingleton();
services.AddAutoMapper(Assembly.GetAssembly(typeof(AutoMapperModule))//如果定义了其他映射配置文件,则也将加载该配置文件。
Configure(Configuration.GetSection(“Auth0”);
var auth0Domain=$“{Configuration[“Auth0:Domain”]}”;
services.AddAuthentication(选项=>
{
options.DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(选项=>
{
options.Authority=auth0Domain;
options.Audience=Configuration[“Auth0:Audience”];
});
我理解错误是说它无法解决依赖项IMemoryCache,通过在启动中添加以下内容,我可以摆脱它:

services.AddSingleton<IMemoryCache, MemoryCache>()
services.AddSingleton()
但让我担心的是,这在早期版本的.NET Core中没有发生,在任何AspNetCoreRateLimit文档中都没有,我真的不知道简单地添加MemoryCache的含义是什么


有谁能帮我找出我遗漏了什么/我做错了什么,以及为什么在.NET Core的新版本中开始出现这种情况,而在.NET Core 2.1中却可以正常工作?

您正在向管道添加一个
伊拉克即时计数器存储库

.AddSingleton()
从中可以看出,
MemoryCacheRateLimitCounterStore
类在其构造函数中采用了
IMemoryCache

公共内存缓存限制计数器存储(IMemoryCache缓存):基本(缓存)
{
}
如果不向管道提供
IMemoryCache
,则无法通过DI构造此类(这就是错误告诉您的)

查看源文件的历史记录,它似乎总是要求构造函数使用该参数。也许,在2.1版中,其他一些服务在幕后添加了一个
IMemoryCache
,但在3.0版中不再为您添加一个


添加内存缓存并没有真正的问题——只要您一直在使用
MemoryCacheRateLimitCounterStore
,它总是以某种方式添加的。似乎您现在只需要自己添加它。

您能列出ConfigService部件的代码吗?AddXYZ部分将在DI中注入服务。我假设在2.0升级到3.0的内部版本时,此注入被删除。顺便说一句,您可以使用services.AddMemeryCache()而不是手动配置IMemeryCache注入。我已经更新了我的问题以包含我的配置代码。这些看起来是您的自定义注入,您还有其他添加部分吗?比如addauthention,addcookies?一些内置的addxyz会注入memorycache,可能会对这些部分进行一些更改,从而使2.1版生效,但3.0版没有。我已经添加了我拥有的所有服务注册码(除了添加授权策略)。不知道这是否有用!