-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathConnectionStringReference.cs
35 lines (28 loc) · 1.36 KB
/
ConnectionStringReference.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// Represents a reference to a connection string.
/// </summary>
public class ConnectionStringReference(IResourceWithConnectionString resource, bool optional) : IManifestExpressionProvider, IValueProvider, IValueWithReferences
{
/// <summary>
/// The resource that the connection string is referencing.
/// </summary>
public IResourceWithConnectionString Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));
/// <summary>
/// A flag indicating whether the connection string is optional.
/// </summary>
public bool Optional { get; } = optional;
string IManifestExpressionProvider.ValueExpression => Resource.ValueExpression;
IEnumerable<object> IValueWithReferences.References => [Resource];
async ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken)
{
var value = await Resource.GetValueAsync(cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(value) && !Optional)
{
throw new DistributedApplicationException($"The connection string for the resource '{Resource.Name}' is not available.");
}
return value;
}
}