fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.19k stars 1.56k forks source link

[BUG] Use Redis cache cause error initialize Localization #717

Closed huanbd closed 1 year ago

huanbd commented 2 years ago

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
fretje commented 2 years ago

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...

AbuHelweh commented 2 years ago

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

fretje commented 2 years ago

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.

khusroohayat commented 2 years ago

@fretje can i work on this?