Repository navigation
Expand file tree
/
Copy pathExternalServiceBuilderExtensions.cs
More file actions
306 lines (262 loc) · 13.5 KB
/
Copy pathExternalServiceBuilderExtensions.cs
File metadata and controls
306 lines (262 loc) · 13.5 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using System.Diagnostics;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using HealthChecks.Uris;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
namespace Aspire.Hosting;
/// <summary>
/// Provides extension methods for adding external services to an application.
/// </summary>
public static class ExternalServiceBuilderExtensions
{
/// <summary>
/// Adds an external service resource to the distributed application with the specified URL.
/// </summary>
/// <param name="builder">The distributed application builder.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="url">The URL of the external service.</param>
/// <returns>An <see cref="IResourceBuilder{ExternalServiceResource}"/> instance.</returns>
public static IResourceBuilder<ExternalServiceResource> AddExternalService(this IDistributedApplicationBuilder builder, [ResourceName] string name, string url)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(url);
if (!ExternalServiceResource.UrlIsValidForExternalService(url, out var uri, out var message))
{
throw new ArgumentException($"The external service URL '{url}' is invalid: {message}", nameof(url));
}
return AddExternalServiceImpl(builder, name, uri);
}
/// <summary>
/// Adds an external service resource to the distributed application with the specified URI.
/// </summary>
/// <param name="builder">The distributed application builder.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="uri">The URI of the external service.</param>
/// <returns>An <see cref="IResourceBuilder{ExternalServiceResource}"/> instance.</returns>
public static IResourceBuilder<ExternalServiceResource> AddExternalService(this IDistributedApplicationBuilder builder, [ResourceName] string name, Uri uri)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(uri);
return AddExternalServiceImpl(builder, name, uri);
}
/// <summary>
/// Adds an external service resource to the distributed application with the URL coming from the specified parameter.
/// </summary>
/// <param name="builder">The distributed application builder.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="urlParameter">The parameter containing the URL of the external service.</param>
/// <returns>An <see cref="IResourceBuilder{ExternalServiceResource}"/> instance.</returns>
public static IResourceBuilder<ExternalServiceResource> AddExternalService(this IDistributedApplicationBuilder builder, [ResourceName] string name, IResourceBuilder<ParameterResource> urlParameter)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(urlParameter);
return AddExternalServiceImpl(builder, name, urlParameter: urlParameter.Resource);
}
private static IResourceBuilder<ExternalServiceResource> AddExternalServiceImpl(IDistributedApplicationBuilder builder, string name, Uri? uri = null, ParameterResource? urlParameter = null)
{
Debug.Assert(uri is not null || urlParameter is not null, "Either uri or urlParameter must be provided.");
var resource = uri is not null
? new ExternalServiceResource(name, uri)
: new ExternalServiceResource(name, urlParameter!);
var resourceBuilder = builder.AddResource(resource)
.WithInitialState(new CustomResourceSnapshot
{
ResourceType = "ExternalService",
State = KnownResourceStates.Waiting,
Properties = []
})
.ExcludeFromManifest();
if (resource.Uri is not null)
{
resourceBuilder.WithUrl(resource.Uri.ToString());
}
else if (resource.UrlParameter is not null)
{
resourceBuilder.WithUrl(ReferenceExpression.Create($"{resource.UrlParameter}"));
}
// Subscribe to the InitializeResourceEvent to finish setting up the resource
builder.Eventing.Subscribe<InitializeResourceEvent>(resource, static async (e, ct) =>
{
var resource = e.Resource as ExternalServiceResource;
if (resource is not null)
{
var uri = resource.Uri;
if (uri is null)
{
// If the URI is not set, it means we are using a parameterized URL
string? url;
try
{
url = resource.UrlParameter is null
? null
: await resource.UrlParameter.GetValueAsync(ct).ConfigureAwait(false);
}
catch (Exception ex)
{
e.Logger.LogError(ex, "Failed to get value for URL parameter '{ParameterName}'", resource.UrlParameter?.Name);
await e.Notifications.PublishUpdateAsync(resource, snapshot => snapshot with
{
State = KnownResourceStates.FailedToStart
}).ConfigureAwait(false);
return;
}
if (!ExternalServiceResource.UrlIsValidForExternalService(url, out uri, out var message))
{
e.Logger.LogError("The value for URL parameter '{ParameterName}' is invalid: {Error}", resource.UrlParameter?.Name, message);
await e.Notifications.PublishUpdateAsync(resource, snapshot => snapshot with
{
State = KnownResourceStates.FailedToStart
}).ConfigureAwait(false);
return;
}
}
Debug.Assert(uri is not null, "URI must be set at this point.");
await e.Eventing.PublishAsync(new BeforeResourceStartedEvent(e.Resource, e.Services), ct).ConfigureAwait(false);
await e.Notifications.PublishUpdateAsync(resource, snapshot => snapshot with
{
Properties = snapshot.Properties.SetResourceProperty(CustomResourceKnownProperties.Source, uri.Host),
// Add the URL if it came from a parameter as non-static URLs must be published by the owning custom resource
Urls = AddUrlIfNotPresent(snapshot.Urls, uri),
// Required in order for health checks to work
State = KnownResourceStates.Running
}).ConfigureAwait(false);
static ImmutableArray<UrlSnapshot> AddUrlIfNotPresent(ImmutableArray<UrlSnapshot> urlSnapshots, Uri uri)
{
if (urlSnapshots.Any(u => string.Equals(u.Url, uri.ToString(), StringComparisons.Url)))
{
return urlSnapshots; // URL already exists, no need to add it again
}
return urlSnapshots.Add(new(Name: null, uri.ToString(), IsInternal: false));
}
}
});
return resourceBuilder;
}
/// <summary>
/// Adds a health check to the external service resource.
/// </summary>
/// <param name="builder"></param>
/// <param name="path">The relative path to use for the HTTP health check.</param>
/// <param name="statusCode"></param>
/// <returns></returns>
/// <remarks>
/// <para>
/// This method adds a health check to the health check service which polls the specified external service
/// on a periodic basis. The address is based on the URL of the external service.
/// A path for the health check request can be specified. The expected status code is set to <c>200</c> by default but a
/// different one can be specified.
/// </para>
/// </remarks>
public static IResourceBuilder<ExternalServiceResource> WithHttpHealthCheck(this IResourceBuilder<ExternalServiceResource> builder, string? path = null, int? statusCode = null)
{
if (path is not null && !Uri.IsWellFormedUriString(path, UriKind.Relative))
{
throw new ArgumentException($"The path '{path}' is not a valid relative URL.", nameof(path));
}
if (builder.Resource.UrlParameter is null)
{
if (builder.Resource.Uri is null)
{
throw new ArgumentException($"The URL for external service '{builder.Resource.Name}' is null.", nameof(builder));
}
else if (builder.Resource.Uri.Scheme != "http" && builder.Resource.Uri.Scheme != "https")
{
throw new ArgumentException($"The URL '{builder.Resource.Uri}' for external service '{builder.Resource.Name}' cannot be used for HTTP health checks because it has a non-HTTP scheme.", nameof(builder));
}
}
statusCode ??= 200;
var pathKey = path is not null ? $"_{path}" : string.Empty;
var healthCheckKey = $"{builder.Resource.Name}_external{pathKey}_{statusCode}_check";
builder.ApplicationBuilder.Services.AddHttpClient();
builder.ApplicationBuilder.Services.SuppressHealthCheckHttpClientLogging(healthCheckKey);
// Check if the external service uses a parameter for its URL
if (builder.Resource.UrlParameter is not null)
{
// For parameter-based URLs, use the custom health check that resolves the URL asynchronously
builder.ApplicationBuilder.Services.AddHealthChecks().Add(new HealthCheckRegistration(
healthCheckKey,
serviceProvider => new ParameterUriHealthCheck(
builder.Resource.UrlParameter,
path,
statusCode.Value,
() => serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(healthCheckKey)),
default,
default,
default));
}
else
{
var uri = builder.Resource.Uri!;
// Use the existing AddUrlGroup approach for static URLs
builder.ApplicationBuilder.Services.AddHealthChecks().AddUrlGroup(options =>
{
var targetUri = uri;
if (path is not null)
{
targetUri = new Uri(uri, path);
}
options.AddUri(targetUri, setup => setup.ExpectHttpCode(statusCode.Value));
}, healthCheckKey);
}
builder.WithHealthCheck(healthCheckKey);
return builder;
}
}
/// <summary>
/// A health check that resolves URL from a parameter asynchronously and delegates to UriHealthCheck.
/// </summary>
internal sealed class ParameterUriHealthCheck : IHealthCheck
{
private readonly ParameterResource _urlParameter;
private readonly Func<HttpClient> _httpClientFactory;
private readonly string? _path;
private readonly int _expectedStatusCode;
private readonly UriHealthCheckOptions _options;
private readonly UriHealthCheck _uriHealthCheck;
public ParameterUriHealthCheck(ParameterResource urlParameter, string? path, int expectedStatusCode, Func<HttpClient> httpClientFactory)
{
_urlParameter = urlParameter ?? throw new ArgumentNullException(nameof(urlParameter));
_path = path;
_expectedStatusCode = expectedStatusCode;
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_options = new UriHealthCheckOptions();
_uriHealthCheck = new UriHealthCheck(_options, _httpClientFactory);
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
// Resolve the URL from the parameter asynchronously
var urlValue = await _urlParameter.GetValueAsync(cancellationToken).ConfigureAwait(false);
// Use ExternalServiceResource validation for the base URL
if (!ExternalServiceResource.UrlIsValidForExternalService(urlValue, out var uri, out var message))
{
return HealthCheckResult.Unhealthy($"The URL '{urlValue}' from parameter '{_urlParameter.Name}' is invalid: {message}");
}
// Additional validation for health check: ensure HTTP/HTTPS scheme
if (uri.Scheme != "http" && uri.Scheme != "https")
{
return HealthCheckResult.Unhealthy($"The URL '{uri}' from parameter '{_urlParameter.Name}' cannot be used for HTTP health checks because it has a non-HTTP scheme.");
}
// Apply path if specified
if (_path is not null)
{
uri = new Uri(uri, _path);
}
_options.AddUri(uri, setup => setup.ExpectHttpCode(_expectedStatusCode));
return await _uriHealthCheck.CheckHealthAsync(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}