AspNetCoreRateLimit icon indicating copy to clipboard operation
AspNetCoreRateLimit copied to clipboard

Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache'

Open cc0d3rr opened this issue 3 years ago • 2 comments

AspNetCoreRateLimit.Redis is not working.

just adding (and some config) var redisOptions = ConfigurationOptions.Parse("dc"); services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(redisOptions)); services.AddRedisRateLimiting(); is not enough:

'Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'AspNetCoreRateLimit.DistributedCacheClientPolicyStore'.'

cc0d3rr avatar Jul 07 '22 08:07 cc0d3rr

Hi! Have you found any way to fix this?

madebyluque avatar Jan 17 '23 01:01 madebyluque

Worked for me in .NET 6 API -

I installed the following nuget packages -

    <PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
    <PackageReference Include="AspNetCoreRateLimit.Redis" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.15" />

In program.cs -

var redisConnectionString = builder.Configuration.GetConnectionString("Redis");
builder.Services.AddStackExchangeRedisCache(o =>
{
    o.Configuration = redisConnectionString;
    o.InstanceName = "AspNetCoreRateLimiting:";
});

builder.Services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(redisConnectionString));
builder.Services.AddRedisRateLimiting();

kunalm8470 avatar Mar 17 '23 17:03 kunalm8470