Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to use LazyCache library with Unity Container?
I want to use LazyCache and UnityContainer together in an ASP.NET classic application (.NET framework 4.6.1+). Namely, to be able to inject IAppCache in various services.
For ASP.NET Core and its default DI container it is as easy as:
services.AddLazyCache();
How can this be done with Unity Container?
Note: this is based on this question and its answers from Stack Overflow, but simplified for the latest version of LazyCache.
1 answer
For LazyCache version 2.1.2 (maybe even earlier) UnityContainer must be instructed to call the default LazyCache constructor which probably relies on MemoryCache (default).
container.RegisterType<IAppCache, CachingService>(new InjectionConstructor());
This worked with .NET Framework 4.6.1, Unity Abstractions 3.1.0.
0 comment threads