From e54b03466763979c29063f842fcb91f6d274e5c2 Mon Sep 17 00:00:00 2001 From: Reuben Bond <203839+ReubenBond@users.noreply.github.com> Date: Tue, 31 Oct 2023 08:42:10 -0700 Subject: [PATCH] Cosmos DB: support custom partition key value providers (#8694) --- .../Orleans.Persistence.Cosmos/CosmosGrainStorage.cs | 12 +++++++++++- .../Orleans.Persistence.Cosmos/HostingExtensions.cs | 6 +++--- src/Orleans.Core/Utils/KeyedService.cs | 9 +++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs b/src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs index 49db39df6a..cb140e60df 100644 --- a/src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs +++ b/src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs @@ -404,6 +404,16 @@ public static class CosmosStorageFactory public static IGrainStorage Create(IServiceProvider services, string name) { var optionsMonitor = services.GetRequiredService>(); - return ActivatorUtilities.CreateInstance(services, name, optionsMonitor.Get(name)); + var partitionKeyProvider = services.GetServiceByName(name) + ?? services.GetRequiredService(); + var loggerFactory = services.GetRequiredService(); + var clusterOptions = services.GetRequiredService>(); + return new CosmosGrainStorage( + name, + optionsMonitor.Get(name), + loggerFactory, + services, + clusterOptions, + partitionKeyProvider); } } \ No newline at end of file diff --git a/src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs b/src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs index 7294276944..624bc721b3 100644 --- a/src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs +++ b/src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs @@ -36,7 +36,7 @@ public static ISiloBuilder AddCosmosGrainStorage( string name, Action configureOptions) where TPartitionKeyProvider : class, IPartitionKeyProvider { - builder.Services.TryAddSingleton(); + builder.Services.AddSingletonNamedService(name); builder.Services.AddCosmosGrainStorage(name, configureOptions); return builder; } @@ -129,7 +129,7 @@ public static ISiloBuilder AddCosmosGrainStorage( string name, Action>? configureOptions = null) where TPartitionKeyProvider : class, IPartitionKeyProvider { - builder.Services.TryAddSingleton(); + builder.Services.AddSingletonNamedService(name); builder.Services.AddCosmosGrainStorage(name, configureOptions); return builder; } @@ -162,7 +162,7 @@ public static ISiloBuilder AddCosmosGrainStorage( { if (customPartitionKeyProviderType != null) { - builder.Services.TryAddSingleton(typeof(IPartitionKeyProvider), customPartitionKeyProviderType); + builder.Services.AddSingletonNamedService(name, customPartitionKeyProviderType); } builder.Services.AddCosmosGrainStorage(name, configureOptions); diff --git a/src/Orleans.Core/Utils/KeyedService.cs b/src/Orleans.Core/Utils/KeyedService.cs index 38ba370b4d..5d5edc1eb9 100644 --- a/src/Orleans.Core/Utils/KeyedService.cs +++ b/src/Orleans.Core/Utils/KeyedService.cs @@ -205,6 +205,15 @@ public static IServiceCollection AddSingletonNamedService(this IServic return collection.AddSingletonKeyedService(name, factory); } + /// + /// Register a singleton named service + /// + public static IServiceCollection AddSingletonNamedService(this IServiceCollection collection, string name, Type implementationType) + where TService : class + { + return collection.AddSingletonKeyedService(name, (sp, name) => (TService)ActivatorUtilities.CreateInstance(sp, implementationType)); + } + /// /// Register a singleton named service ///