Skip to content

Commit 5915eec

Browse files
fix(configuration): fix broken configuration for .net8 (#28)
1 parent 11ae4cc commit 5915eec

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.DependencyInjection.Extensions;
1+
#nullable enable
2+
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Logging;
44
using Microsoft.Extensions.Options;
55
using Newtonsoft.Json;
@@ -9,8 +9,7 @@
99
using Orleans.Persistence.Redis.Config;
1010
using Orleans.Persistence.Redis.Core;
1111
using Orleans.Persistence.Redis.Serialization;
12-
using Orleans.Providers;
13-
using Orleans.Runtime;
12+
using Orleans.Runtime.Hosting;
1413
using Orleans.Serialization;
1514
using Orleans.Storage;
1615
using JsonSerializer = Orleans.Persistence.Redis.Serialization.JsonSerializer;
@@ -21,10 +20,8 @@ namespace Orleans.Hosting;
2120

2221
public static class RedisSiloBuilderExtensions
2322
{
24-
public static RedisStorageOptionsBuilder AddRedisGrainStorage(
25-
this ISiloBuilder builder,
26-
string name
27-
) => new RedisStorageOptionsBuilder(builder, name);
23+
public static RedisStorageOptionsBuilder AddRedisGrainStorage(this ISiloBuilder builder, string name)
24+
=> new(builder, name);
2825

2926
public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault(
3027
this ISiloBuilder builder
@@ -33,21 +30,59 @@ this ISiloBuilder builder
3330
internal static IServiceCollection AddRedisGrainStorage(
3431
this IServiceCollection services,
3532
string name,
36-
Action<OptionsBuilder<RedisStorageOptions>> configureOptions = null
33+
Action<OptionsBuilder<RedisStorageOptions>>? configureOptions = null
3734
)
3835
{
3936
configureOptions?.Invoke(services.AddOptions<RedisStorageOptions>(name));
40-
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
41-
services.AddKeyedSingleton(name, CreateStateStore);
4237
services.ConfigureNamedOptionForLogging<RedisStorageOptions>(name);
43-
services.TryAddSingleton(sp =>
44-
sp.GetKeyedService<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
4538

39+
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
4640
return services
47-
.AddKeyedSingleton(name, CreateDbConnection)
48-
.AddKeyedSingleton(name, CreateRedisStorage)
49-
.AddKeyedSingleton(name, (provider, n)
50-
=> (ILifecycleParticipant<ISiloLifecycle>)provider.GetRequiredKeyedService<IGrainStorage>(n));
41+
.AddKeyedSingleton<IGrainStateStore>(name, (sp, k) =>
42+
{
43+
var key = (string)k;
44+
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
45+
var serializer = sp.GetRequiredKeyedService<ISerializer>(key);
46+
var humanReadableSerializer = sp.GetKeyedService<IHumanReadableSerializer>(key);
47+
var options = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
48+
var logger = sp.GetRequiredService<ILogger<GrainStateStore>>();
49+
50+
return ActivatorUtilities.CreateInstance<GrainStateStore>(
51+
sp,
52+
key,
53+
connection,
54+
options.Get(key),
55+
serializer,
56+
humanReadableSerializer,
57+
logger,
58+
sp
59+
);
60+
}
61+
)
62+
.AddKeyedSingleton(name, (sp, k) =>
63+
{
64+
var key = (string)k;
65+
var optionsSnapshot = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
66+
var logger = sp.GetRequiredService<ILogger<DbConnection>>();
67+
return ActivatorUtilities.CreateInstance<DbConnection>(sp, optionsSnapshot.Get(key), logger);
68+
}
69+
)
70+
.AddKeyedSingleton<IGrainStorage>(name, (sp, k) =>
71+
{
72+
var key = (string)k;
73+
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
74+
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
75+
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);
76+
}
77+
)
78+
.AddGrainStorage(name, (sp, key) =>
79+
{
80+
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
81+
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
82+
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);
83+
}
84+
)
85+
;
5186
}
5287

5388
internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name)
@@ -99,40 +134,6 @@ Func<IServiceProvider, object[]> cfg
99134
services.AddKeyedSingleton<IHumanReadableSerializer>(name, (provider, n)
100135
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, cfg?.Invoke(provider)))
101136
);
102-
103-
private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name)
104-
{
105-
var store = services.GetRequiredKeyedService<IGrainStateStore>(name);
106-
var connection = services.GetRequiredKeyedService<DbConnection>(name);
107-
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(services, name, store, connection);
108-
}
109-
110-
private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name)
111-
{
112-
var connection = provider.GetRequiredKeyedService<DbConnection>(name);
113-
var serializer = provider.GetRequiredKeyedService<ISerializer>(name);
114-
var humanReadableSerializer = provider.GetKeyedServices<IHumanReadableSerializer>(name);
115-
var options = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
116-
var logger = provider.GetRequiredService<ILogger<GrainStateStore>>();
117-
118-
return ActivatorUtilities.CreateInstance<GrainStateStore>(
119-
provider,
120-
name,
121-
connection,
122-
options.Get(name),
123-
serializer,
124-
humanReadableSerializer,
125-
logger,
126-
provider
127-
);
128-
}
129-
130-
private static DbConnection CreateDbConnection(IServiceProvider provider, string name)
131-
{
132-
var optionsSnapshot = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
133-
var logger = provider.GetRequiredService<ILogger<DbConnection>>();
134-
return ActivatorUtilities.CreateInstance<DbConnection>(provider, optionsSnapshot.Get(name), logger);
135-
}
136137
}
137138

138139
public static class RedisDefaultJsonSerializerSettings

0 commit comments

Comments
 (0)