Reputation: 83
I was trying to create user secrets in application console in c# in last days but I wasn't able to generate the file secret .
I ended up followed this tutoriel and finally was able to create secret.xml . But when I tried to read data from the secret.xml file, it gave me this error :
Thrown exception: 'System.Configuration.ConfigurationErrorsException' in System.Configuration.dll
An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll
An error occurred while loading a configuration file: Unable to load the file or assembly 'Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version = 1.0.0.0, Culture = neutral' or one of its dependencies. The specified file can not be found.
At the beginning I was able to read the password data from the config file , but when I tried to read it from secret file it seemsnot woriking .
Solution tried : -removed the reference to UserSecrets then added it again using Nuget .
Here is my code : click here
App.config :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
</configSections>
<configBuilders>
<builders>
<add name="Secrets" userSecretsFile="c:\\secrets\\secret.xml" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=1.0.0.0, Culture=neutral"/>
</builders>
</configBuilders>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<appSettings configBuilders="Secrets">
<add key="password" value="This is a password placeholder"/>
</appSettings>
</configuration>
Program.cs :
static void Main(string[] args)
{
var password = ConfigurationManager.AppSettings["password"];
Console.WriteLine($"Password is {password}");
}
Upvotes: 1
Views: 4092
Reputation: 5986
Based on my test, I find the solution. You can try change the current .net framework
version to 4.7.1 or later.
Click properties-> choose target framework-> choose .net Framework 4.7.2
like the following:
Then, you can refer to the app.config I test.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
</configSections>
<configBuilders>
<builders>
<add name="Secrets" userSecretsFile="D:\\secrets.xml" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/></builders>
</configBuilders>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<appSettings configBuilders="Secrets">
<add key="password" value="This is a password placeholder"/>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Configuration.ConfigurationBuilders.Base" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
You can know .net framework has been changed to v4.7.2.
Finally, I can get the correct result from the console app.
Upvotes: 2