Reputation: 788
I use to set some environment variable in order to overwrite appsettings values that are specific to my dev machine (ie connection strings, local endpoints etc). I set them in User variables for my account for example Variable Name: ConnectionStrings:DbConnectionString, Variable value: ******************
Then I call in Main method (of a .NET core 3.1 web app):
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
var env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
This works pretty fine overwriting appsettings values in connectionstrings and in few custom Options classes, but I can't make it read GUID values. I tried writing them with or without double apices, but they're are ignored, while they're correctly loaded and parsed from appsettings.
If I change the type of the property in my Options class to String then the GUID is loaded as string from env variables as expected.
So, as a workaround, I ended up loading the value in a string property and converting it to GUID in a different property of the Options class.
It works, but I'd like to understand whether there's a cleaner way to do that. Is there a special syntax to write GUID in env variables?
Thanks
Upvotes: 1
Views: 357
Reputation: 788
After some more tests, I found out the issue was about the syntax of the Guid: while in appsettings file you can write it with hyphens (ie 0713d50d-e508-4212-b174-7582a5c90224), in windows env variable the value has to be written without hyphens and without quotes, either single or double (ie ee1b55626c8148aab62cc67dc7f6010e).
In this way it's correctly read a parsed as Guid (if it's a valid Guid of course)
Upvotes: 1