1
- using Microsoft . Extensions . DependencyInjection ;
2
- using Microsoft . Extensions . DependencyInjection . Extensions ;
1
+ #nullable enable
2
+ using Microsoft . Extensions . DependencyInjection ;
3
3
using Microsoft . Extensions . Logging ;
4
4
using Microsoft . Extensions . Options ;
5
5
using Newtonsoft . Json ;
9
9
using Orleans . Persistence . Redis . Config ;
10
10
using Orleans . Persistence . Redis . Core ;
11
11
using Orleans . Persistence . Redis . Serialization ;
12
- using Orleans . Providers ;
13
- using Orleans . Runtime ;
12
+ using Orleans . Runtime . Hosting ;
14
13
using Orleans . Serialization ;
15
14
using Orleans . Storage ;
16
15
using JsonSerializer = Orleans . Persistence . Redis . Serialization . JsonSerializer ;
@@ -21,10 +20,8 @@ namespace Orleans.Hosting;
21
20
22
21
public static class RedisSiloBuilderExtensions
23
22
{
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 ) ;
28
25
29
26
public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault (
30
27
this ISiloBuilder builder
@@ -33,21 +30,59 @@ this ISiloBuilder builder
33
30
internal static IServiceCollection AddRedisGrainStorage (
34
31
this IServiceCollection services ,
35
32
string name ,
36
- Action < OptionsBuilder < RedisStorageOptions > > configureOptions = null
33
+ Action < OptionsBuilder < RedisStorageOptions > > ? configureOptions = null
37
34
)
38
35
{
39
36
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 ) ;
42
37
services . ConfigureNamedOptionForLogging < RedisStorageOptions > ( name ) ;
43
- services . TryAddSingleton ( sp =>
44
- sp . GetKeyedService < IGrainStorage > ( ProviderConstants . DEFAULT_STORAGE_PROVIDER_NAME ) ) ;
45
38
39
+ // services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
46
40
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
+ ;
51
86
}
52
87
53
88
internal static ISiloBuilder AddRedisDefaultSerializer ( this ISiloBuilder builder , string name )
@@ -99,40 +134,6 @@ Func<IServiceProvider, object[]> cfg
99
134
services . AddKeyedSingleton < IHumanReadableSerializer > ( name , ( provider , n )
100
135
=> ActivatorUtilities . CreateInstance < TSerializer > ( provider , cfg ? . Invoke ( provider ) ) )
101
136
) ;
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
- }
136
137
}
137
138
138
139
public static class RedisDefaultJsonSerializerSettings
0 commit comments