-
Notifications
You must be signed in to change notification settings - Fork 330
/
DistributedApplicationBuilder.cs
363 lines (308 loc) · 16.9 KB
/
DistributedApplicationBuilder.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Dashboard;
using Aspire.Hosting.Dcp;
using Aspire.Hosting.Lifecycle;
using Aspire.Hosting.Publishing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Aspire.Hosting;
/// <summary>
/// A builder for creating instances of <see cref="DistributedApplication"/>.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="DistributedApplicationBuilder"/> is the primary implementation of
/// <see cref="IDistributedApplicationBuilder"/> within .NET Aspire. Typically a developer
/// would interact with instances of this class via the <see cref="IDistributedApplicationBuilder"/>
/// interface which was created using one of the <see cref="DistributedApplication.CreateBuilder(string[])"/>
/// overloads.
/// </para>
/// <para>
/// For more information on how to configure the <see cref="DistributedApplication" /> using the
/// the builder pattern see <see cref="IDistributedApplicationBuilder" />.
/// </para>
/// </remarks>
public class DistributedApplicationBuilder : IDistributedApplicationBuilder
{
private const string HostingDiagnosticListenerName = "Aspire.Hosting";
private const string ApplicationBuildingEventName = "DistributedApplicationBuilding";
private const string ApplicationBuiltEventName = "DistributedApplicationBuilt";
private const string BuilderConstructingEventName = "DistributedApplicationBuilderConstructing";
private const string BuilderConstructedEventName = "DistributedApplicationBuilderConstructed";
private readonly HostApplicationBuilder _innerBuilder;
/// <inheritdoc />
public IHostEnvironment Environment => _innerBuilder.Environment;
/// <inheritdoc />
public ConfigurationManager Configuration => _innerBuilder.Configuration;
/// <inheritdoc />
public IServiceCollection Services => _innerBuilder.Services;
/// <inheritdoc />
public string AppHostDirectory { get; }
/// <inheritdoc />
public DistributedApplicationExecutionContext ExecutionContext { get; }
/// <inheritdoc />
public IResourceCollection Resources { get; } = new ResourceCollection();
/// <summary>
/// Initializes a new instance of the <see cref="DistributedApplicationBuilder"/> class with the specified options.
/// </summary>
/// <param name="args">The arguments provided to the builder.</param>
/// <remarks>
/// <para>
/// Developers will not typically construct an instance of the <see cref="DistributedApplicationBuilder"/>
/// class themselves and will instead use the <see cref="DistributedApplication.CreateBuilder(string[])"/>.
/// This constructor is public to allow for some testing around extensibility scenarios.
/// </para>
/// </remarks>
public DistributedApplicationBuilder(string[] args) : this(new DistributedApplicationOptions { Args = args })
{
ArgumentNullException.ThrowIfNull(args);
}
/// <summary>
/// Initializes a new instance of the <see cref="DistributedApplicationBuilder"/> class with the specified options.
/// </summary>
/// <param name="options">The options for the distributed application.</param>
/// <remarks>
/// <para>
/// Developers will not typically construct an instance of the <see cref="DistributedApplicationBuilder"/>
/// class themselves and will instead use the <see cref="DistributedApplication.CreateBuilder(string[])"/>.
/// This constructor is public to allow for some testing around extensibility scenarios.
/// </para>
/// <para>
/// This constructor generates an instance of the <see cref="IDistributedApplicationBuilder"/> interface
/// which is very similar to the instance that is returned from <see cref="DistributedApplication.CreateBuilder(string[])"/>
/// however it is not guaranteed to be 100% consistent. For typical usage it is recommended that the
/// <see cref="DistributedApplication.CreateBuilder(string[])"/> method is to create instances of
/// the <see cref="IDistributedApplicationBuilder"/> interface.
/// </para>
/// </remarks>
public DistributedApplicationBuilder(DistributedApplicationOptions options)
{
ArgumentNullException.ThrowIfNull(options);
var innerBuilderOptions = new HostApplicationBuilderSettings();
LogBuilderConstructing(options, innerBuilderOptions);
_innerBuilder = new HostApplicationBuilder(innerBuilderOptions);
_innerBuilder.Logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Warning);
_innerBuilder.Logging.AddFilter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Error);
_innerBuilder.Logging.AddFilter("Aspire.Hosting.Dashboard", LogLevel.None);
// This is so that we can see certificate errors in the resource server in the console logs.
// See: https://github.com/dotnet/aspire/issues/2914
_innerBuilder.Logging.AddFilter("Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer", LogLevel.Warning);
// Add the logging configuration again to allow the user to override the defaults
_innerBuilder.Logging.AddConfiguration(_innerBuilder.Configuration.GetSection("Logging"));
AppHostDirectory = options.ProjectDirectory ?? _innerBuilder.Environment.ContentRootPath;
// Set configuration
ConfigurePublishingOptions(options);
_innerBuilder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
// Make the app host directory available to the application via configuration
["AppHost:Directory"] = AppHostDirectory
});
ExecutionContext = _innerBuilder.Configuration["Publishing:Publisher"] switch
{
"manifest" => new DistributedApplicationExecutionContext(DistributedApplicationOperation.Publish),
_ => new DistributedApplicationExecutionContext(DistributedApplicationOperation.Run)
};
// Core things
_innerBuilder.Services.AddSingleton(sp => new DistributedApplicationModel(Resources));
_innerBuilder.Services.AddHostedService<DistributedApplicationLifecycle>();
_innerBuilder.Services.AddHostedService<DistributedApplicationRunner>();
_innerBuilder.Services.AddSingleton(options);
_innerBuilder.Services.AddSingleton<ResourceNotificationService>();
_innerBuilder.Services.AddSingleton<ResourceLoggerService>();
if (ExecutionContext.IsRunMode)
{
// Dashboard
if (!options.DisableDashboard)
{
if (!IsDashboardUnsecured(_innerBuilder.Configuration))
{
// Set a random API key for the OTLP exporter.
// Passed to apps as a standard OTEL attribute to include in OTLP requests and the dashboard to validate.
_innerBuilder.Configuration.AddInMemoryCollection(
new Dictionary<string, string?>
{
["AppHost:OtlpApiKey"] = TokenGenerator.GenerateToken()
}
);
// Determine the frontend browser token.
if (_innerBuilder.Configuration[KnownConfigNames.DashboardFrontendBrowserToken] is not { Length: > 0 } browserToken)
{
// No browser token was specified in configuration, so generate one.
browserToken = TokenGenerator.GenerateToken();
}
_innerBuilder.Configuration.AddInMemoryCollection(
new Dictionary<string, string?>
{
["AppHost:BrowserToken"] = browserToken
}
);
// Determine the resource service API key.
if (_innerBuilder.Configuration[KnownConfigNames.DashboardResourceServiceClientApiKey] is not { Length: > 0 } apiKey)
{
// No API key was specified in configuration, so generate one.
apiKey = TokenGenerator.GenerateToken();
}
_innerBuilder.Configuration.AddInMemoryCollection(
new Dictionary<string, string?>
{
["AppHost:ResourceService:AuthMode"] = nameof(ResourceServiceAuthMode.ApiKey),
["AppHost:ResourceService:ApiKey"] = apiKey
}
);
}
_innerBuilder.Services.AddOptions<TransportOptions>().ValidateOnStart().PostConfigure(MapTransportOptionsFromCustomKeys);
_innerBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<TransportOptions>, TransportOptionsValidator>());
_innerBuilder.Services.AddSingleton<DashboardServiceHost>();
_innerBuilder.Services.AddHostedService(sp => sp.GetRequiredService<DashboardServiceHost>());
_innerBuilder.Services.AddSingleton<IDashboardEndpointProvider, HostDashboardEndpointProvider>();
_innerBuilder.Services.AddLifecycleHook<DashboardLifecycleHook>();
_innerBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<DashboardOptions>, ConfigureDefaultDashboardOptions>());
_innerBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<DashboardOptions>, ValidateDashboardOptions>());
}
// DCP stuff
_innerBuilder.Services.AddSingleton<ApplicationExecutor>();
_innerBuilder.Services.AddSingleton<IDcpDependencyCheckService, DcpDependencyCheck>();
_innerBuilder.Services.AddHostedService<DcpHostService>();
_innerBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<DcpOptions>, ConfigureDefaultDcpOptions>());
_innerBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<DcpOptions>, ValidateDcpOptions>());
// We need a unique path per application instance
_innerBuilder.Services.AddSingleton(new Locations());
_innerBuilder.Services.AddSingleton<IKubernetesService, KubernetesService>();
}
// Publishing support
_innerBuilder.Services.AddLifecycleHook<Http2TransportMutationHook>();
_innerBuilder.Services.AddLifecycleHook<DashboardManifestExclusionHook>();
_innerBuilder.Services.AddKeyedSingleton<IDistributedApplicationPublisher, ManifestPublisher>("manifest");
// Overwrite registry if override specified in options
if (!string.IsNullOrEmpty(options.ContainerRegistryOverride))
{
_innerBuilder.Services.AddLifecycleHook<ContainerRegistryHook>();
}
_innerBuilder.Services.AddSingleton(ExecutionContext);
LogBuilderConstructed(this);
}
private void MapTransportOptionsFromCustomKeys(TransportOptions options)
{
if (Configuration.GetBool(KnownConfigNames.AllowUnsecuredTransport) is { } allowUnsecuredTransport)
{
options.AllowUnsecureTransport = allowUnsecuredTransport;
}
}
private static bool IsDashboardUnsecured(IConfiguration configuration)
{
return configuration.GetBool(KnownConfigNames.DashboardUnsecuredAllowAnonymous) ?? false;
}
private void ConfigurePublishingOptions(DistributedApplicationOptions options)
{
var switchMappings = new Dictionary<string, string>()
{
{ "--publisher", "Publishing:Publisher" },
{ "--output-path", "Publishing:OutputPath" },
{ "--dcp-cli-path", "DcpPublisher:CliPath" },
{ "--container-runtime", "DcpPublisher:ContainerRuntime" },
{ "--dependency-check-timeout", "DcpPublisher:DependencyCheckTimeout" },
};
_innerBuilder.Configuration.AddCommandLine(options.Args ?? [], switchMappings);
_innerBuilder.Services.Configure<PublishingOptions>(_innerBuilder.Configuration.GetSection(PublishingOptions.Publishing));
}
/// <inheritdoc />
public DistributedApplication Build()
{
AspireEventSource.Instance.DistributedApplicationBuildStart();
try
{
LogAppBuilding(this);
// AddResource(resource) validates that a name is unique but it's possible to add resources directly to the resource collection.
// Validate names for duplicates while building the application.
foreach (var duplicateResourceName in Resources.GroupBy(r => r.Name, StringComparers.ResourceName)
.Where(g => g.Count() > 1)
.Select(g => g.Key))
{
throw new DistributedApplicationException($"Multiple resources with the name '{duplicateResourceName}'. Resource names are case-insensitive.");
}
var application = new DistributedApplication(_innerBuilder.Build());
LogAppBuilt(application);
return application;
}
finally
{
AspireEventSource.Instance.DistributedApplicationBuildStop();
}
}
/// <inheritdoc />
public IResourceBuilder<T> AddResource<T>(T resource) where T : IResource
{
ArgumentNullException.ThrowIfNull(resource);
if (Resources.FirstOrDefault(r => string.Equals(r.Name, resource.Name, StringComparisons.ResourceName)) is { } existingResource)
{
throw new DistributedApplicationException($"Cannot add resource of type '{resource.GetType()}' with name '{resource.Name}' because resource of type '{existingResource.GetType()}' with that name already exists. Resource names are case-insensitive.");
}
Resources.Add(resource);
return CreateResourceBuilder(resource);
}
/// <inheritdoc />
public IResourceBuilder<T> CreateResourceBuilder<T>(T resource) where T : IResource
{
ArgumentNullException.ThrowIfNull(resource);
var builder = new DistributedApplicationResourceBuilder<T>(this, resource);
return builder;
}
private static DiagnosticListener LogBuilderConstructing(DistributedApplicationOptions appBuilderOptions, HostApplicationBuilderSettings hostBuilderOptions)
{
var diagnosticListener = new DiagnosticListener(HostingDiagnosticListenerName);
if (diagnosticListener.IsEnabled() && diagnosticListener.IsEnabled(BuilderConstructingEventName))
{
Write(diagnosticListener, BuilderConstructingEventName, (appBuilderOptions, hostBuilderOptions));
}
return diagnosticListener;
}
private static DiagnosticListener LogBuilderConstructed(DistributedApplicationBuilder builder)
{
var diagnosticListener = new DiagnosticListener(HostingDiagnosticListenerName);
if (diagnosticListener.IsEnabled() && diagnosticListener.IsEnabled(BuilderConstructedEventName))
{
Write(diagnosticListener, BuilderConstructedEventName, builder);
}
return diagnosticListener;
}
private static DiagnosticListener LogAppBuilding(DistributedApplicationBuilder appBuilder)
{
var diagnosticListener = new DiagnosticListener(HostingDiagnosticListenerName);
if (diagnosticListener.IsEnabled() && diagnosticListener.IsEnabled(ApplicationBuildingEventName))
{
Write(diagnosticListener, ApplicationBuildingEventName, appBuilder);
}
return diagnosticListener;
}
private static DiagnosticListener LogAppBuilt(DistributedApplication app)
{
var diagnosticListener = new DiagnosticListener(HostingDiagnosticListenerName);
if (diagnosticListener.IsEnabled() && diagnosticListener.IsEnabled(ApplicationBuiltEventName))
{
Write(diagnosticListener, ApplicationBuiltEventName, app);
}
return diagnosticListener;
}
// Remove when https://github.com/dotnet/runtime/pull/78532 is merged and consumed by the used SDK.
#if NET7_0
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode",
Justification = "DiagnosticSource is used here to pass objects in-memory to code using HostFactoryResolver. This won't require creating new generic types.")]
#endif
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = "The values being passed into Write are being consumed by the application already.")]
private static void Write<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(
DiagnosticListener diagnosticSource,
string name,
T value)
{
diagnosticSource.Write(name, value);
}
}