From 5915eecd815eca25641489ec1f0fce541a81f83a Mon Sep 17 00:00:00 2001 From: Stephen Lautier Date: Wed, 14 Feb 2024 18:14:28 +0100 Subject: [PATCH] fix(configuration): fix broken configuration for .net8 (#28) --- .../Config/RedisSiloHostBuilderExtensions.cs | 103 +++++++++--------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs b/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs index 0f090c0..047706a 100644 --- a/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs +++ b/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs @@ -1,5 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; +#nullable enable +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; @@ -9,8 +9,7 @@ using Orleans.Persistence.Redis.Config; using Orleans.Persistence.Redis.Core; using Orleans.Persistence.Redis.Serialization; -using Orleans.Providers; -using Orleans.Runtime; +using Orleans.Runtime.Hosting; using Orleans.Serialization; using Orleans.Storage; using JsonSerializer = Orleans.Persistence.Redis.Serialization.JsonSerializer; @@ -21,10 +20,8 @@ namespace Orleans.Hosting; public static class RedisSiloBuilderExtensions { - public static RedisStorageOptionsBuilder AddRedisGrainStorage( - this ISiloBuilder builder, - string name - ) => new RedisStorageOptionsBuilder(builder, name); + public static RedisStorageOptionsBuilder AddRedisGrainStorage(this ISiloBuilder builder, string name) + => new(builder, name); public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault( this ISiloBuilder builder @@ -33,21 +30,59 @@ this ISiloBuilder builder internal static IServiceCollection AddRedisGrainStorage( this IServiceCollection services, string name, - Action> configureOptions = null + Action>? configureOptions = null ) { configureOptions?.Invoke(services.AddOptions(name)); - // services.AddTransient(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService>().Get(name), name)); - services.AddKeyedSingleton(name, CreateStateStore); services.ConfigureNamedOptionForLogging(name); - services.TryAddSingleton(sp => - sp.GetKeyedService(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); + // services.AddTransient(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService>().Get(name), name)); return services - .AddKeyedSingleton(name, CreateDbConnection) - .AddKeyedSingleton(name, CreateRedisStorage) - .AddKeyedSingleton(name, (provider, n) - => (ILifecycleParticipant)provider.GetRequiredKeyedService(n)); + .AddKeyedSingleton(name, (sp, k) => + { + var key = (string)k; + var connection = sp.GetRequiredKeyedService(key); + var serializer = sp.GetRequiredKeyedService(key); + var humanReadableSerializer = sp.GetKeyedService(key); + var options = sp.GetRequiredService>(); + var logger = sp.GetRequiredService>(); + + return ActivatorUtilities.CreateInstance( + sp, + key, + connection, + options.Get(key), + serializer, + humanReadableSerializer, + logger, + sp + ); + } + ) + .AddKeyedSingleton(name, (sp, k) => + { + var key = (string)k; + var optionsSnapshot = sp.GetRequiredService>(); + var logger = sp.GetRequiredService>(); + return ActivatorUtilities.CreateInstance(sp, optionsSnapshot.Get(key), logger); + } + ) + .AddKeyedSingleton(name, (sp, k) => + { + var key = (string)k; + var store = sp.GetRequiredKeyedService(key); + var connection = sp.GetRequiredKeyedService(key); + return ActivatorUtilities.CreateInstance(sp, key, store, connection); + } + ) + .AddGrainStorage(name, (sp, key) => + { + var store = sp.GetRequiredKeyedService(key); + var connection = sp.GetRequiredKeyedService(key); + return ActivatorUtilities.CreateInstance(sp, key, store, connection); + } + ) + ; } internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name) @@ -99,40 +134,6 @@ Func cfg services.AddKeyedSingleton(name, (provider, n) => ActivatorUtilities.CreateInstance(provider, cfg?.Invoke(provider))) ); - - private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name) - { - var store = services.GetRequiredKeyedService(name); - var connection = services.GetRequiredKeyedService(name); - return ActivatorUtilities.CreateInstance(services, name, store, connection); - } - - private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name) - { - var connection = provider.GetRequiredKeyedService(name); - var serializer = provider.GetRequiredKeyedService(name); - var humanReadableSerializer = provider.GetKeyedServices(name); - var options = provider.GetRequiredService>(); - var logger = provider.GetRequiredService>(); - - return ActivatorUtilities.CreateInstance( - provider, - name, - connection, - options.Get(name), - serializer, - humanReadableSerializer, - logger, - provider - ); - } - - private static DbConnection CreateDbConnection(IServiceProvider provider, string name) - { - var optionsSnapshot = provider.GetRequiredService>(); - var logger = provider.GetRequiredService>(); - return ActivatorUtilities.CreateInstance(provider, optionsSnapshot.Get(name), logger); - } } public static class RedisDefaultJsonSerializerSettings