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
117 lines
4.3 KiB
Markdown
117 lines
4.3 KiB
Markdown
# AppSettings
|
|
|
|
## User Secrets Initialization
|
|
|
|
- [app-secrets](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-9.0&tabs=windows)
|
|
|
|
```bash 1733926424710 = 638695232247100000 = Wed Dec 11 2024 07:13:44 GMT-0700 (Mountain Standard Time)
|
|
dotnet user-secrets -p File-Folder-Helper.csproj init
|
|
```
|
|
|
|
- [app-secrets](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-9.0&tabs=windows)
|
|
|
|
## User Secrets Add Entry
|
|
|
|
```bash 1733926491404 = 638695232914040000 = Wed Dec 11 2024 07:14:50 GMT-0700 (Mountain Standard Time)
|
|
dotnet user-secrets -p File-Folder-Helper.csproj set Name Value
|
|
```
|
|
|
|
## Optional Symbolic Link in Windows
|
|
|
|
- Similar to Linux command ```ln -s```
|
|
|
|
```bash 1733926521307 = 638695233213070000 = Wed Dec 11 2024 07:15:20 GMT-0700 (Mountain Standard Time)
|
|
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
|
|
|
|
```xml 1733926548840 = 638695233488400000 = Wed Dec 11 2024 07:15:48 GMT-0700 (Mountain Standard Time)
|
|
<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
|
|
|
|
```csharp 1733926601507 = 638695234015070000 = Wed Dec 11 2024 07:16:41 GMT-0700 (Mountain Standard Time)
|
|
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
|
|
|
|
```csharp 1733926619366 = 638695234193660000 = Wed Dec 11 2024 07:16:58 GMT-0700 (Mountain Standard Time)
|
|
builder.Configuration.AddUserSecrets<Program>();
|
|
```
|
|
|
|
## Get Instance of Record to Program.cs
|
|
|
|
```csharp 1733926659168 = 638695234591680000 = Wed Dec 11 2024 07:17:38 GMT-0700 (Mountain Standard Time)
|
|
AppSettings appSettings = AppSettings.Get(builder.Configuration);
|
|
```
|
|
|
|
## Add Instance to be Used as Dependency Injected Object
|
|
|
|
```csharp 1733926695948 = 638695234959480000 = Wed Dec 11 2024 07:18:15 GMT-0700 (Mountain Standard Time)
|
|
builder.Services.AddSingleton(appSettings);
|
|
```
|