-
Notifications
You must be signed in to change notification settings - Fork 347
/
DistributedApplicationOptions.cs
96 lines (81 loc) · 3.27 KB
/
DistributedApplicationOptions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection;
namespace Aspire.Hosting;
/// <summary>
/// Options for configuring the behavior of <see cref="DistributedApplication.CreateBuilder(DistributedApplicationOptions)"/>.
/// </summary>
public sealed class DistributedApplicationOptions
{
private readonly Lazy<Assembly?> _assembly;
private readonly Lazy<string?> _projectDirectoryLazy;
// This is for testing
private string? _projectDirectory;
/// <summary>
/// Initializes a new instance of <see cref="DistributedApplicationOptions"/>.
/// </summary>
public DistributedApplicationOptions()
{
_assembly = new(ResolveAssembly);
_projectDirectoryLazy = new(ResolveProjectDirectory);
}
/// <summary>
/// When containers are used, use this value instead to override the container registry
/// that is specified.
/// </summary>
public string? ContainerRegistryOverride { get; set; }
/// <summary>
/// The command line arguments.
/// </summary>
public string[]? Args { get; set; }
/// <summary>
/// The AssemblyName of the AppHost project for loading configuration attributes; if not set defaults to Assembly.GetEntryAssembly().
/// </summary>
public string? AssemblyName { get; set; }
/// <summary>
/// Determines whether the dashboard is disabled.
/// </summary>
public bool DisableDashboard { get; set; }
internal Assembly? Assembly => _assembly.Value;
internal string? ProjectDirectory
{
get => _projectDirectory ?? _projectDirectoryLazy.Value;
set => _projectDirectory = value;
}
internal bool DashboardEnabled => !DisableDashboard;
/// <summary>
/// Allows the use of HTTP urls for for the AppHost resource endpoint.
/// </summary>
public bool AllowUnsecuredTransport { get; set; }
private string? ResolveProjectDirectory()
{
var assemblyMetadata = Assembly?.GetCustomAttributes<AssemblyMetadataAttribute>();
return GetMetadataValue(assemblyMetadata, "AppHostProjectPath");
}
private Assembly? ResolveAssembly()
{
// Calculate DCP locations from configuration options
var appHostAssembly = Assembly.GetEntryAssembly();
if (!string.IsNullOrEmpty(AssemblyName))
{
try
{
// Find an assembly in the current AppDomain with the given name
appHostAssembly = Assembly.Load(AssemblyName);
if (appHostAssembly == null)
{
throw new FileNotFoundException($"No assembly with name '{AssemblyName}' exists in the current AppDomain.");
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to load AppHost assembly '{AssemblyName}' specified in {nameof(DistributedApplicationOptions)}.", ex);
}
}
return appHostAssembly;
}
private static string? GetMetadataValue(IEnumerable<AssemblyMetadataAttribute>? assemblyMetadata, string key)
{
return assemblyMetadata?.FirstOrDefault(m => string.Equals(m.Key, key, StringComparison.OrdinalIgnoreCase))?.Value;
}
}