fi-operations/.NET/app-settings.md
Mike Phares 3a3f5bcd02 Update SSL Certificate Request
VSCode SSH
- Allows using VSCode to build code as if you were on remote machine.
- Remote machine will still need dotnet SDK

Added AppSetting Markdown
2025-04-02 16:13:03 -07:00

4.3 KiB

AppSettings

User Secrets Initialization

dotnet user-secrets -p File-Folder-Helper.csproj init

User Secrets Add Entry

dotnet user-secrets -p File-Folder-Helper.csproj set Name Value
  • Similar to Linux command ln -s
mklink /J "L:\DevOps\Mesa_FI\File-Folder-Helper\.vscode\.UserSecrets" "C:\Users\phares\AppData\Roaming\Microsoft\UserSecrets\8da397d4-13ec-4576-9722-3c79cad25563"

.NET Package Reference

<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />

Example Record (Class)

  • ToString is optional for recursive debug and isn't used in code
  • Verify method helps debug where project is looking for configuration entries
  • configurationRoot.Get<AppSettings>(); is builtin method to deserialize
  • JsonSerializerContext is optional for using AOT builds
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace File_Folder_Helper.Models;

public record AppSettings(string Company,
                          string DefaultNoteType,
                          string[] ExcludeDirectoryNames,
                          string[] ExcludeSchemes,
                          string PersonBirthdayFormat,
                          string[] ValidImageFormatExtensions,
                          string WorkingDirectoryName)
{

    public override string ToString()
    {
        string result = JsonSerializer.Serialize(this, AppSettingsSourceGenerationContext.Default.AppSettings);
        return result;
    }

    private static void Verify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
    {
        if (appSettings?.Company is null || string.IsNullOrEmpty(appSettings.Company))
        {
            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())}");
        }
    }

    public static AppSettings Get(IConfigurationRoot configurationRoot)
    {
        AppSettings? result;
#pragma warning disable IL3050, IL2026
        result = configurationRoot.Get<AppSettings>();
#pragma warning restore IL3050, IL2026
        Verify(configurationRoot, result);
        if (result is null)
            throw new Exception("Not set!");
        return result;
    }

}

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(AppSettings))]
internal partial class AppSettingsSourceGenerationContext : JsonSerializerContext
{
}

AddUserSecrets to Program.cs

builder.Configuration.AddUserSecrets<Program>();

Get Instance of Record to Program.cs

AppSettings appSettings = AppSettings.Get(builder.Configuration);

Add Instance to be Used as Dependency Injected Object

builder.Services.AddSingleton(appSettings);