Unable to resolve service for type 'AspNetCoreRateLimit.IProcessingStrategy' while attempting to activate 'AspNetCoreRateLimit.IpRateLimitMiddleware'
Asked Answered
L

2

13

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"?

Lila answered 23/8, 2021 at 7:13 Comment(0)
L
27

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

Lawford answered 13/9, 2021 at 5:8 Comment(0)
O
2

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.

Oxytocic answered 2/1, 2023 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.