question

EuroEager2008-7858 avatar image
0 Votes"
EuroEager2008-7858 asked EuroEager2008-7858 commented

Appsettings.json relation to Solution Configuration

Have a WPF app (VS2022 .Net 6 and Prism 8)
Want an appsettings.debug.json or similar to override the appsettings.json without changing the launch profile (just by the Debug or Release (or more) configuration.
Anyone knows how to do?

Using something like the following to build the configuration, but that relies on changed DOTNET_ENVIRONMENT variable.

 IConfigurationRoot configurationRoot = new ConfigurationBuilder()
     .SetBasePath(Directory.GetCurrentDirectory())
     .AddJsonFile("appsettings.json")
     .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
     .Build();


dotnet-csharpvs-debugging
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered EuroEager2008-7858 commented

In general, the Environment.GetEnvironmentVariable is really designed for running on different servers, dev, staging, prod. WPF does not have a launchSettings.json file which does allow you to swap out to a different environment like with ASP.NET and set on the start app button in Visual Studio.

You could try something like the following in the pre-build event where you set Configuration for Conditional compilation symbol in the Build tab of project properties.

copy appSettings.$(Configuration).json appSettings.json

Edit, see this code sample which targets connections strings but can be adapted for other settings.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have launchsettings.json in my WPF project

0 Votes 0 ·

Could you please expose some sample?

0 Votes 0 ·
EuroEager2008-7858 avatar image
0 Votes"
EuroEager2008-7858 answered

Thanks
Meanwhile I did it by #if DEBUG #else preprocessor directives
As you mention conditional compilation I remember doing this some time ago (other project) and it worked ok.
The last suggestion seems to work as well, but I don't like the approach (selection code in production output).

I think it is a bit strange that VS2022 doesn't have something easier for this, I refuse to believe I am alone needing this.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.