When debugging only app.Services.GetRequiredService<IPCRBService>(); Injected AppSettings instead of using GetEnvironmentVariable at Services level Get ready to use VSCode IDE
93 lines
5.1 KiB
C#
93 lines
5.1 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace MesaFabApproval.Models;
|
|
|
|
public record AppSettings(string Company,
|
|
string DbConnectionString,
|
|
string JwtAudience,
|
|
string JwtIssuer,
|
|
string JwtKey,
|
|
string MrbAttachmentPath,
|
|
string PcrbAttachmentPath,
|
|
bool ShouldSendEmail,
|
|
string SiteBaseUrl,
|
|
string WorkingDirectoryName) {
|
|
|
|
public override string ToString() {
|
|
string result = JsonSerializer.Serialize(this, AppSettingsSourceGenerationContext.Default.AppSettings);
|
|
return result;
|
|
}
|
|
|
|
public static AppSettings Get(IConfigurationRoot configurationRoot) {
|
|
AppSettings? result;
|
|
try {
|
|
#pragma warning disable IL3050, IL2026
|
|
result = configurationRoot.Get<AppSettings>() ?? throw new Exception();
|
|
#pragma warning restore IL3050, IL2026
|
|
} catch (Exception) {
|
|
List<string> paths = [];
|
|
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers) {
|
|
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
|
|
continue;
|
|
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
|
|
continue;
|
|
paths.Add(physicalFileProvider.Root);
|
|
}
|
|
|
|
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
internal static void SetEnvironmentVariables(WebApplicationBuilder builder) {
|
|
builder.Configuration.AddUserSecrets<Program>();
|
|
AppSettings appSettings = Get(builder.Configuration);
|
|
Environment.SetEnvironmentVariable("FabApprovalDbConnectionString", appSettings.DbConnectionString);
|
|
Environment.SetEnvironmentVariable("FabApprovalJwtAudience", appSettings.JwtAudience);
|
|
Environment.SetEnvironmentVariable("FabApprovalJwtIssuer", appSettings.JwtIssuer);
|
|
Environment.SetEnvironmentVariable("FabApprovalJwtKey", appSettings.JwtKey);
|
|
Environment.SetEnvironmentVariable("FabApprovalMrbAttachmentPath", appSettings.MrbAttachmentPath);
|
|
Environment.SetEnvironmentVariable("FabApprovalPcrbAttachmentPath", appSettings.PcrbAttachmentPath);
|
|
Environment.SetEnvironmentVariable("FabApprovalShouldSendEmail", appSettings.ShouldSendEmail.ToString());
|
|
Environment.SetEnvironmentVariable("NewFabApprovalBaseUrl", appSettings.SiteBaseUrl);
|
|
}
|
|
|
|
internal static AppSettings LoadEnvironmentVariables() {
|
|
AppSettings result;
|
|
string dbConnectionString = Environment.GetEnvironmentVariable("FabApprovalDbConnectionString") ??
|
|
throw new ArgumentNullException("FabApprovalDbConnectionString environment variable not found");
|
|
string jwtAudience = Environment.GetEnvironmentVariable("FabApprovalJwtAudience") ??
|
|
throw new ArgumentNullException("FabApprovalJwtAudience environment variable not found");
|
|
string jwtIssuer = Environment.GetEnvironmentVariable("FabApprovalJwtIssuer") ??
|
|
throw new ArgumentNullException("FabApprovalJwtIssuer environment variable not found");
|
|
string jwtKey = Environment.GetEnvironmentVariable("FabApprovalJwtKey") ??
|
|
throw new ArgumentNullException("FabApprovalJwtKey environment variable not found");
|
|
string mrbAttachmentPath = Environment.GetEnvironmentVariable("FabApprovalMrbAttachmentPath") ??
|
|
throw new ArgumentNullException("FabApprovalMrbAttachmentPath environment variable not found");
|
|
string pcrbAttachmentPath = Environment.GetEnvironmentVariable("FabApprovalPcrbAttachmentPath") ??
|
|
throw new ArgumentNullException("FabApprovalPcrbAttachmentPath environment variable not found");
|
|
if (!bool.TryParse(Environment.GetEnvironmentVariable("FabApprovalShouldSendEmail"), out bool shouldSendEmail))
|
|
throw new ArgumentNullException("FabApprovalShouldSendEmail environment variable not found");
|
|
string siteBaseUrl = Environment.GetEnvironmentVariable("NewFabApprovalBaseUrl") ??
|
|
throw new ArgumentNullException("FabApprovalBaseUrl environment variable not found");
|
|
result = new("Infineon Technologies Americas Corp.",
|
|
DbConnectionString: dbConnectionString,
|
|
JwtAudience: jwtAudience,
|
|
JwtIssuer: jwtIssuer,
|
|
JwtKey: jwtKey,
|
|
MrbAttachmentPath: mrbAttachmentPath,
|
|
PcrbAttachmentPath: pcrbAttachmentPath,
|
|
ShouldSendEmail: shouldSendEmail,
|
|
SiteBaseUrl: siteBaseUrl,
|
|
"IFXApps");
|
|
return result;
|
|
}
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(AppSettings))]
|
|
internal partial class AppSettingsSourceGenerationContext : JsonSerializerContext {
|
|
} |