22namespace Microsoft . Extensions . DependencyInjection
33{
44 using System ;
5+ using System . Linq ;
56 using Neolution . Extensions . Caching . Abstractions ;
67 using Neolution . Extensions . Caching . Distributed ;
78
@@ -11,39 +12,82 @@ namespace Microsoft.Extensions.DependencyInjection
1112 public static class ServiceCollectionExtensions
1213 {
1314 /// <summary>
14- /// Adds the distributed caching implementation that uses MessagePack for serialization.
15+ /// Adds the serialized distributed caching implementation with MessagePack serialization.
1516 /// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
1617 /// provider to be registered (e.g., Redis, SQL Server, Memory).
1718 /// </summary>
1819 /// <param name="services">The service collection.</param>
1920 /// <returns>The service collection for fluent chaining.</returns>
20- public static IServiceCollection AddMessagePackDistributedCache ( this IServiceCollection services )
21+ /// <exception cref="InvalidOperationException">Thrown when no IDistributedCache provider is registered.</exception>
22+ public static IServiceCollection AddSerializedDistributedCache ( this IServiceCollection services )
2123 {
22- return services . AddMessagePackDistributedCache ( _ => { } ) ;
24+ return services . AddSerializedDistributedCache ( _ => { } ) ;
2325 }
2426
2527 /// <summary>
26- /// Adds the distributed caching implementation that uses MessagePack for serialization,
27- /// with custom configuration options.
28+ /// Adds the serialized distributed caching implementation with MessagePack serialization
29+ /// and custom configuration options.
2830 /// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
2931 /// provider to be registered (e.g., Redis, SQL Server, Memory).
3032 /// </summary>
3133 /// <param name="services">The service collection.</param>
3234 /// <param name="configureOptions">The action to configure cache options.</param>
3335 /// <returns>The service collection for fluent chaining.</returns>
3436 /// <exception cref="ArgumentNullException">Thrown when configureOptions is null.</exception>
35- public static IServiceCollection AddMessagePackDistributedCache ( this IServiceCollection services , Action < MessagePackDistributedCacheOptions > configureOptions )
37+ /// <exception cref="InvalidOperationException">Thrown when no IDistributedCache provider is registered.</exception>
38+ public static IServiceCollection AddSerializedDistributedCache ( this IServiceCollection services , Action < MessagePackDistributedCacheOptions > configureOptions )
3639 {
3740 if ( configureOptions == null )
3841 {
3942 throw new ArgumentNullException ( nameof ( configureOptions ) ) ;
4043 }
4144
45+ // Validate that an IDistributedCache provider has been registered
46+ if ( ! services . Any ( x => x . ServiceType == typeof ( Microsoft . Extensions . Caching . Distributed . IDistributedCache ) ) )
47+ {
48+ throw new InvalidOperationException (
49+ """
50+ An IDistributedCache provider must be registered before calling AddSerializedDistributedCache().
51+ Register a provider such as Redis (AddStackExchangeRedisCache), SQL Server (AddDistributedSqlServerCache),
52+ or Memory (AddDistributedMemoryCache) first.
53+ """
54+ ) ;
55+ }
56+
4257 services . AddOptions ( ) ;
4358 services . Configure ( configureOptions ) ;
4459 services . AddSingleton ( typeof ( IDistributedCache < > ) , typeof ( MessagePackDistributedCache < > ) ) ;
4560
4661 return services ;
4762 }
63+
64+ /// <summary>
65+ /// Adds the distributed caching implementation that uses MessagePack for serialization.
66+ /// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
67+ /// provider to be registered (e.g., Redis, SQL Server, Memory).
68+ /// </summary>
69+ /// <param name="services">The service collection.</param>
70+ /// <returns>The service collection for fluent chaining.</returns>
71+ [ Obsolete ( "Use AddSerializedDistributedCache() instead. This method will be removed in a future version." ) ]
72+ public static IServiceCollection AddMessagePackDistributedCache ( this IServiceCollection services )
73+ {
74+ return services . AddSerializedDistributedCache ( ) ;
75+ }
76+
77+ /// <summary>
78+ /// Adds the distributed caching implementation that uses MessagePack for serialization,
79+ /// with custom configuration options.
80+ /// Requires an <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
81+ /// provider to be registered (e.g., Redis, SQL Server, Memory).
82+ /// </summary>
83+ /// <param name="services">The service collection.</param>
84+ /// <param name="configureOptions">The action to configure cache options.</param>
85+ /// <returns>The service collection for fluent chaining.</returns>
86+ /// <exception cref="ArgumentNullException">Thrown when configureOptions is null.</exception>
87+ [ Obsolete ( "Use AddSerializedDistributedCache() instead. This method will be removed in a future version." ) ]
88+ public static IServiceCollection AddMessagePackDistributedCache ( this IServiceCollection services , Action < MessagePackDistributedCacheOptions > configureOptions )
89+ {
90+ return services . AddSerializedDistributedCache ( configureOptions ) ;
91+ }
4892 }
4993}
0 commit comments