I have used the package AspNetCoreRateLimit Version="4.0.1" and I get the following exception: Unable to resolve service for type 'AspNetCoreRateLimit.IProcessingStrategy' while attempting to activate 'AspNetCoreRateLimit.IpRateLimitMiddleware' but when I use the package AspNetCoreRateLimit Version="3.2.2" it works. Although Version="4.0.1" is the latest stable version I am not being able to use it. What sort of bugs can I expect later if I keep on using Version="3.2.2"?
Unable to resolve service for type 'AspNetCoreRateLimit.IProcessingStrategy' while attempting to activate 'AspNetCoreRateLimit.IpRateLimitMiddleware'
According to the author of AspNetCoreRateLimit, you have to add following line of code in Startup.cs
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
It seems as they no longer have a default IProcessingStrategy registered. See here: https://github.com/stefanprodan/AspNetCoreRateLimit/issues/236#issuecomment-883311511
In the new versions, some code is embedded in the method,
public static IServiceCollection AddInMemoryRateLimiting(this IServiceCollection services)
{
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IClientPolicyStore, MemoryCacheClientPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
return services;
}
Therefore, it will be sufficient to add the following services;
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMemoryCache();
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
services.AddInMemoryRateLimiting();
services.AddMvc();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
}
You can get information about changes and option differences from here.
© 2022 - 2024 — McMap. All rights reserved.