Skip to content

.NET v4.8: updating the Microsoft.Configuration.ConfigurationBuilders to version 3.0 does not work #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AlgorithmicThoughts opened this issue Feb 12, 2025 · 2 comments

Comments

@AlgorithmicThoughts
Copy link

I am working on a .NET 4.8 application, I updated the packages Microsoft.Configuration.ConfigurationBuilders (Environment, Json, UserSecrets) from version 2.0 to 3.0. I have also updated my app.config file to reflect the latest version, shown here:

<configBuilders>
    <builders>
      <add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="Secrets" 
           userSecretsFile="./App_Data/secrets.xml" 
           mode="Greedy" 
           type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>      
    </builders>
  </configBuilders>

The structure of the secrets.xml file is given below:

<root>
<secrets ver="1.0">
    <secret name="App:UserKey" value="Hello World"/>
</secrets>
</root>

Before the update, when the version was 2.0. I was able to read the value from the secrets.xml file specified in the app.config key userSecretsFile:

var userKey = System.Configuration.ConfigurationManager.AppSettings["App:UserKey"];

After the update from version 2 to 3, this code no longer works and returns an empty string. I read the update guide but none of it provides troubleshooting guidance. I tried to update the path from ./App_Data/secrets.xml to ~/App_Data/secrets.xml for testing even then I get an empty string in the variable userKey.

Is there an additional step that needs to be performed in version 3 to make it work? Please suggest thanks

@StephenMolloy
Copy link
Contributor

Sorry for the slow response.

I assume from the 'App_Data' part of your path that this is in an ASP.Net application. In which case, ~/App_Data/secrets.xml should be the correct way to go in your case. And I believe that should also be the case for non-ASP.Net apps as well in 3.0.

I do find it curious that your config file has a raw default value for the secret and then when you read the appSetting in your code you get an empty string. That suggests to me that something else is going on here - because if the builder failed to find your secret file, it should just leave the default Hello World alone. Did you by chance figure this out on your own?

Incidentally, the 'User Secrets' part of this config builder name kind of implies that it is bringing secrets into the application. I would recommend against puting secrets in a file within the application itself like you're doing here. Secrets should always live somewhere outside the application and only be brought in when appropriate. (Maybe you're just testing or maybe the data you've got in your secrets.xml isn't actually secret. But it would be irresponsible of me to not say anything. 😉)

@AlgorithmicThoughts
Copy link
Author

Thank you for the response @StephenMolloy
How I was able to resolve this was in Visual Studio I right-clicked the file and updated the property Copy to Output Directory from Do not copy to Copy if newer and it worked but I didn't have to do this in the previous version? Is this an undocumented step that needs to happen for version 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants