dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

[BUG] Use Redis cache cause error initialize Localization

Open huanbd opened this issue 3 years ago • 4 comments

Describe the bug When I change cache settings to use Distributed cache Redis. It cause error initialize Localization

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder': Unable to resolve service for type 'Microsoft.Extensions.Caching.Memory.IMemoryCache' while attempting to activate 'OrchardCore.Localization.LocalizationManager'.)

To Reproduce Steps to reproduce the behavior:

  1. Change setting in /Configurations/cache.json { "CacheSettings": { "UseDistributedCache": true, "PreferRedis": true, "RedisURL": "localhost:6379" } }
  2. Restart
  3. See error

huanbd avatar Jun 24 '22 02:06 huanbd

seems like the call to services.AddMemoryCache() in the AddCaching method here: https://github.com/fullstackhero/dotnet-webapi-boilerplate/blob/370dc7bb7a5c84af53ea37cdffa109060b07807e/src/Infrastructure/Caching/Startup.cs#L36

will have to go outside of the if/else block...

fretje avatar Jun 24 '22 19:06 fretje

Thank you that solve the exception, How about the next line services.AddTransient<ICacheService, LocalCacheService>(); should be moved as well or its ok inside the else?

Regards

AbuHelweh avatar Jul 11 '22 20:07 AbuHelweh

No, if you enable redis, ICacheService is the RedisCacheService, otherwise it's the LocalCacheService... this lets you seamlessly switch between local and remote cache... The local cache is using IMemoryCache internally, but apparently IMemoryCache is also used by some other components (outside of the LocalCacheService). That's why that one needs to be registered always.

If you only want always local memory cache (irregardless of using reddis) it's best to use IMemoryCache itself in stead of ICacheService.

fretje avatar Jul 12 '22 07:07 fretje

@fretje can i work on this?

khusroohayat avatar Jul 18 '22 16:07 khusroohayat