forked from dotnet/orleans
/
ClientBuilderExtensions.cs
286 lines (267 loc) · 13.7 KB
/
ClientBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Linq;
using System.Net;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Orleans.Configuration;
using Orleans.Hosting;
using Orleans.ApplicationParts;
using Orleans.CodeGeneration;
using Orleans.Messaging;
using Orleans.Runtime;
namespace Orleans
{
/// <summary>
/// Extension methods for <see cref="IClientBuilder"/>.
/// </summary>
public static class ClientBuilderExtensions
{
/// <summary>
/// Configures default client services.
/// </summary>
/// <param name="builder">The host builder.</param>
/// <returns>The client builder.</returns>
public static IClientBuilder ConfigureDefaults(this IClientBuilder builder)
{
// Configure the container to use an Orleans client.
builder.ConfigureServices(services =>
{
const string key = "OrleansClientServicesAdded";
if (!builder.Properties.ContainsKey(key))
{
DefaultClientServices.AddDefaultServices(builder, services);
builder.Properties.Add(key, true);
}
});
return builder;
}
/// <summary>
/// Specify the environment to be used by the host.
/// </summary>
/// <param name="hostBuilder">The host builder to configure.</param>
/// <param name="environment">The environment to host the application in.</param>
/// <returns>The host builder.</returns>
public static IClientBuilder UseEnvironment(this IClientBuilder hostBuilder, string environment)
{
return hostBuilder.ConfigureHostConfiguration(configBuilder =>
{
configBuilder.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(HostDefaults.EnvironmentKey,
environment ?? throw new ArgumentNullException(nameof(environment)))
});
});
}
/// <summary>
/// Adds services to the container. This can be called multiple times and the results will be additive.
/// </summary>
/// <param name="hostBuilder">The <see cref="IClientBuilder" /> to configure.</param>
/// <param name="configureDelegate"></param>
/// <returns>The same instance of the <see cref="IClientBuilder"/> for chaining.</returns>
public static IClientBuilder ConfigureServices(this IClientBuilder hostBuilder, Action<IServiceCollection> configureDelegate)
{
return hostBuilder.ConfigureServices((context, collection) => configureDelegate(collection));
}
/// <summary>
/// Sets up the configuration for the remainder of the build process and application. This can be called multiple times and
/// the results will be additive. The results will be available at <see cref="HostBuilderContext.Configuration"/> for
/// subsequent operations./>.
/// </summary>
/// <param name="hostBuilder">The host builder to configure.</param>
/// <param name="configureDelegate"></param>
/// <returns>The same instance of the host builder for chaining.</returns>
public static IClientBuilder ConfigureAppConfiguration(this IClientBuilder hostBuilder, Action<IConfigurationBuilder> configureDelegate)
{
return hostBuilder.ConfigureAppConfiguration((context, builder) => configureDelegate(builder));
}
/// <summary>
/// Registers an action used to configure a particular type of options.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="builder">The host builder.</param>
/// <param name="configureOptions">The action used to configure the options.</param>
/// <returns>The client builder.</returns>
public static IClientBuilder Configure<TOptions>(this IClientBuilder builder, Action<TOptions> configureOptions) where TOptions : class
{
return builder.ConfigureServices(services => services.Configure(configureOptions));
}
/// <summary>
/// Registers a configuration instance which <typeparamref name="TOptions"/> will bind against.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="builder">The host builder.</param>
/// <param name="configuration">The configuration.</param>
/// <returns>The client builder.</returns>
public static IClientBuilder Configure<TOptions>(this IClientBuilder builder, IConfiguration configuration) where TOptions : class
{
return builder.ConfigureServices(services => services.AddOptions<TOptions>().Bind(configuration));
}
/// <summary>
/// Adds a client invocation callback.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="callback">The callback.</param>
/// <remarks>
/// A <see cref="ClientInvokeCallback"/> is a global pre-call interceptor.
/// Synchronous callback made just before a message is about to be constructed and sent by a client to a grain.
/// This call will be made from the same thread that constructs the message to be sent, so any thread-local settings
/// such as <c>Orleans.RequestContext</c> will be picked up.
/// The action receives an <see cref="InvokeMethodRequest"/> with details of the method to be invoked, including InterfaceId and MethodId,
/// and a <see cref="IGrain"/> which is the GrainReference this request is being sent through
/// This callback method should return promptly and do a minimum of work, to avoid blocking calling thread or impacting throughput.
/// </remarks>
/// <returns>The builder.</returns>
public static IClientBuilder AddClientInvokeCallback(this IClientBuilder builder, ClientInvokeCallback callback)
{
builder.ConfigureServices(services => services.AddSingleton(callback));
return builder;
}
/// <summary>
/// Registers a <see cref="ConnectionToClusterLostHandler"/> event handler.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="handler">The handler.</param>
/// <returns>The builder.</returns>
public static IClientBuilder AddClusterConnectionLostHandler(this IClientBuilder builder, ConnectionToClusterLostHandler handler)
{
builder.ConfigureServices(services => services.AddSingleton(handler));
return builder;
}
/// <summary>
/// Specifies how the <see cref="IServiceProvider"/> for this client is configured.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="factory">The service provider factory.</param>
/// <returns>The builder.</returns>
public static IClientBuilder UseServiceProviderFactory(this IClientBuilder builder, Func<IServiceCollection, IServiceProvider> factory)
{
builder.UseServiceProviderFactory(new DelegateServiceProviderFactory(factory));
return builder;
}
/// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggingBuilder"/>. This may be called multiple times.
/// </summary>
/// <param name="builder">The <see cref="IClientBuilder" /> to configure.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggingBuilder"/>.</param>
/// <returns>The same instance of the <see cref="IClientBuilder"/> for chaining.</returns>
public static IClientBuilder ConfigureLogging(this IClientBuilder builder, Action<ILoggingBuilder> configureLogging)
{
return builder.ConfigureServices(collection => collection.AddLogging(loggingBuilder => configureLogging(loggingBuilder)));
}
/// <summary>
/// Configures the client to connect to a silo on the localhost.
/// </summary>
/// <param name="builder"></param>
/// <param name="gatewayPort">The local silo's gateway port.</param>
/// <param name="serviceId">The service id.</param>
/// <param name="clusterId">The cluster id.</param>
public static IClientBuilder UseLocalhostClustering(
this IClientBuilder builder,
int gatewayPort = 30000,
string serviceId = ClusterOptions.DevelopmentServiceId,
string clusterId = ClusterOptions.DevelopmentClusterId)
{
return builder.UseLocalhostClustering(new [] {gatewayPort}, serviceId, clusterId);
}
/// <summary>
/// Configures the client to connect to a silo on the localhost.
/// </summary>
/// <param name="builder"></param>
/// <param name="gatewayPorts">The local silo gateway ports.</param>
/// <param name="serviceId">The service id.</param>
/// <param name="clusterId">The cluster id.</param>
public static IClientBuilder UseLocalhostClustering(this IClientBuilder builder,
int[] gatewayPorts,
string serviceId = ClusterOptions.DevelopmentServiceId,
string clusterId = ClusterOptions.DevelopmentClusterId)
{
return builder.UseStaticClustering(gatewayPorts.Select(p => new IPEndPoint(IPAddress.Loopback, p)).ToArray())
.ConfigureServices(services =>
{
// If the caller did not override service id or cluster id, configure default values as a fallback.
if (string.Equals(serviceId, ClusterOptions.DevelopmentServiceId) && string.Equals(clusterId, ClusterOptions.DevelopmentClusterId))
{
services.PostConfigure<ClusterOptions>(options =>
{
if (string.IsNullOrWhiteSpace(options.ClusterId)) options.ClusterId = ClusterOptions.DevelopmentClusterId;
if (string.IsNullOrWhiteSpace(options.ServiceId)) options.ServiceId = ClusterOptions.DevelopmentServiceId;
});
}
else
{
services.Configure<ClusterOptions>(options =>
{
options.ServiceId = serviceId;
options.ClusterId = clusterId;
});
}
});
}
/// <summary>
/// Configures the client to use static clustering.
/// </summary>
/// <param name="builder"></param>
/// <param name="endpoints">The gateway endpoints.</param>
public static IClientBuilder UseStaticClustering(this IClientBuilder builder, params IPEndPoint[] endpoints)
{
return builder.UseStaticClustering(options => options.Gateways = endpoints.Select(ep => ep.ToGatewayUri()).ToList());
}
/// <summary>
/// Configures the client to use static clustering.
/// </summary>
public static IClientBuilder UseStaticClustering(this IClientBuilder builder, Action<StaticGatewayListProviderOptions> configureOptions)
{
return builder.ConfigureServices(
collection =>
{
if (configureOptions != null)
{
collection.Configure(configureOptions);
}
collection.AddSingleton<IGatewayListProvider, StaticGatewayListProvider>()
.ConfigureFormatter<StaticGatewayListProviderOptions>();
});
}
/// <summary>
/// Configures the client to use static clustering.
/// </summary>
public static IClientBuilder UseStaticClustering(this IClientBuilder builder, Action<OptionsBuilder<StaticGatewayListProviderOptions>> configureOptions)
{
return builder.ConfigureServices(
collection =>
{
configureOptions?.Invoke(collection.AddOptions<StaticGatewayListProviderOptions>());
collection.AddSingleton<IGatewayListProvider, StaticGatewayListProvider>()
.ConfigureFormatter<StaticGatewayListProviderOptions>();
});
}
/// <summary>
/// Returns the <see cref="ApplicationPartManager"/> for this builder.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns>The <see cref="ApplicationPartManager"/> for this builder.</returns>
public static IApplicationPartManager GetApplicationPartManager(this IClientBuilder builder) => ApplicationPartManagerExtensions.GetApplicationPartManager(builder.Properties);
/// <summary>
/// Configures the <see cref="ApplicationPartManager"/> for this builder.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="configure">The configuration delegate.</param>
/// <returns>The builder.</returns>
public static IClientBuilder ConfigureApplicationParts(this IClientBuilder builder, Action<IApplicationPartManager> configure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
configure(builder.GetApplicationPartManager());
return builder;
}
}
}