Skip to content

Commit 81db420

Browse files
Extended SecretAppSettingReader
Fixed #408
1 parent 1befc9a commit 81db420

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- Source: https://github.com/ncarandini/XFUserSecrets/blob/master/TPCWare.XFUserSecrets/Directory.Build.targets-->
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Target Name="AddUserSecrets"
4+
BeforeTargets="PrepareForBuild"
5+
Condition="'$(UserSecretsId)' != '' ">
6+
<PropertyGroup>
7+
<UserSecretsFilePath Condition=" '$(OS)' == 'Windows_NT' ">
8+
$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))\AppData\Roaming\Microsoft\UserSecrets\$(UserSecretsId)\secrets.json
9+
</UserSecretsFilePath>
10+
<UserSecretsFilePath Condition=" '$(OS)' == 'Unix' ">
11+
$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))/.microsoft/usersecrets/$(UserSecretsId)/secrets.json
12+
</UserSecretsFilePath>
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<EmbeddedResource Include="$(UserSecretsFilePath)" Condition="Exists($(UserSecretsFilePath))"/>
16+
</ItemGroup>
17+
</Target>
18+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Core": {
3+
"GitUrl": "",
4+
"GitToken": "",
5+
"GitLocalPath": ""
6+
}
7+
}

src/SharedNetCoreLibrary/Utilities/SecretAppSettingReader.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#if NEWTONSOFT
22
using Newtonsoft.Json;
33
#endif
4+
using System.Reflection;
45

56
namespace AndreasReitberger.Shared.Core.Utilities
67
{
78
public class SecretAppSettingReader
89
{
10+
#region Properties
11+
public static Assembly? Assembly { get; set; }
12+
#endregion
13+
914
// Source: https://www.programmingwithwolfgang.com/use-net-secrets-in-console-application/
1015
#if NEWTONSOFT
1116
public static T? ReadSection<T>(string sectionName)
@@ -22,5 +27,26 @@ public class SecretAppSettingReader
2227
return (T?)JsonSerializer.Deserialize(settings, typeof(T), context);
2328
#endif
2429
}
30+
31+
#if !NEWTONSOFT
32+
public static T? ReadSectionFromConfigurationRoot<T>(Type type, string appNameSpace, string sectionName, JsonSerializerContext? context = null)
33+
{
34+
// It seems that this way makes problems if the app is published on Windows in Release mode
35+
// Needs the Directory.Build.targets in order to work (copies the secret.json as EmbeddedResource to the app)
36+
if (Assembly is null)
37+
{
38+
Assembly = IntrospectionExtensions.GetTypeInfo(type).Assembly;
39+
UserSecretsManager.Settings = new UserSecretsManager.UserSecretsManagerBuilder()
40+
.WithAppNamespace(appNameSpace)
41+
.WithCustomAssambly(Assembly)
42+
.Build();
43+
}
44+
context ??= CoreSourceGenerationContext.Default;
45+
string settings = UserSecretsManager.Settings[sectionName].ToString();
46+
if (string.IsNullOrEmpty(settings))
47+
return default;
48+
return (T?)JsonSerializer.Deserialize(settings, typeof(T), context);
49+
}
2550
}
51+
#endif
2652
}

0 commit comments

Comments
 (0)