Skip to content

Commit 9a5697a

Browse files
committed
Fix caching adapter activation for serializer selection
1 parent 996bdfe commit 9a5697a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/CleanArchitecture.Extensions.Caching/Adapters/DistributedCacheAdapter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CleanArchitecture.Extensions.Caching.Options;
55
using CleanArchitecture.Extensions.Caching.Serialization;
66
using Microsoft.Extensions.Caching.Distributed;
7+
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Logging;
89
using Microsoft.Extensions.Options;
910

@@ -47,6 +48,7 @@ public DistributedCacheAdapter(
4748
/// <param name="options">Caching options.</param>
4849
/// <param name="timeProvider">Time provider used for timestamps.</param>
4950
/// <param name="logger">Logger.</param>
51+
[ActivatorUtilitiesConstructor]
5052
public DistributedCacheAdapter(
5153
IDistributedCache distributedCache,
5254
IEnumerable<ICacheSerializer> serializers,

src/CleanArchitecture.Extensions.Caching/Adapters/MemoryCacheAdapter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CleanArchitecture.Extensions.Caching.Options;
55
using CleanArchitecture.Extensions.Caching.Serialization;
66
using Microsoft.Extensions.Caching.Memory;
7+
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Logging;
89
using Microsoft.Extensions.Options;
910

@@ -47,6 +48,7 @@ public MemoryCacheAdapter(
4748
/// <param name="timeProvider">Time provider used for timestamps.</param>
4849
/// <param name="options">Caching options.</param>
4950
/// <param name="logger">Logger.</param>
51+
[ActivatorUtilitiesConstructor]
5052
public MemoryCacheAdapter(
5153
IMemoryCache memoryCache,
5254
IEnumerable<ICacheSerializer> serializers,

src/CleanArchitecture.Extensions.Caching/DependencyInjectionExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ public static IServiceCollection AddCleanArchitectureCaching(
4747
services.TryAddSingleton<ICacheKeyFactory, DefaultCacheKeyFactory>();
4848
// Call AddCleanArchitectureMultitenancyCaching to bind cache scopes to tenant context.
4949
services.TryAddScoped<ICacheScope, DefaultCacheScope>();
50-
services.TryAddSingleton<ICache, MemoryCacheAdapter>();
51-
services.TryAddSingleton<DistributedCacheAdapter>();
50+
services.TryAddSingleton<ICache>(sp =>
51+
ActivatorUtilities.CreateInstance<MemoryCacheAdapter>(sp, sp.GetServices<ICacheSerializer>()));
52+
services.TryAddSingleton<DistributedCacheAdapter>(sp =>
53+
ActivatorUtilities.CreateInstance<DistributedCacheAdapter>(sp, sp.GetServices<ICacheSerializer>()));
5254

5355
return services;
5456
}

0 commit comments

Comments
 (0)