Using appsettings.json in UWP

ASP.NET Core was the first to introduce the replacement for the traditional XML-based configuration in .NET via App.config and Web.config, which we used since… well, always. The modern configuration system Microsoft.Extensions.Configuration.* is easily extensible and flexible. Even better – as there is no dependency on the ASP.NET Core infrastructure, it can easily be used in any .NET app! In this article, we will see how to set up a UWP app to use JSON-based configuration through appsettings.json file.

Installing the NuGets

All of the modern configuration libraries can be installed easily from NuGet package manager. For our purposes, we need the following packages:

The 3.x versions of these packages support .NET Standard 2.0, so they are easily portable.

Setting up the configuration

To contain all the configuration logic in one place, we create an AppConfig class. In its constructor, we need to set up the configuration pipeline. As the JSON file is deployed with the application package, we set the base path for file searching to Package.Current.InstalledLocation.Path. Then we add appsettings.json to the pipeline:

private readonly IConfigurationRoot _configurationRoot;
public AppConfig()
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false);
_configurationRoot = builder.Build();

Now we need to add a appsettings.json file to our project. We will have a single example setting there:

"example": {
"message" : "Hello from appsettings.json!"

view raw
hosted with ❤ by GitHub

Important note – make sure the appsettings.json file has its Build Action set to Content and its Copy to Output Directory to Copy Always in the properties, so that it is indeed deployed with the application. We can do so by selecting the file and adjusting its options in the Properties window:

Ensure the json file is set as Content file
Ensure the JSON file is set as Content file

Retrieving configuration sections

To make it easy to retrieve strongly-typed configuration sections, we prepare a simple helper method:

private T GetSection<T>(string key) => _configurationRoot.GetSection(key).Get<T>();

view raw
hosted with ❤ by GitHub

Now we cover our “example” section with a simple POCO class:

public class Example
public string Message { get; set; }

view raw
hosted with ❤ by GitHub

And create a property to retrieve this section:

public Example Example => GetSection<Example>(nameof(Example));

view raw
hosted with ❤ by GitHub

That’s all there is to it! Now we can simply use the AppConfig class, anywhere we need it:

var config = new AppConfig();
var contentDialog = new ContentDialog()
Content = config.Example.Message,
CloseButtonText = "OK"
await contentDialog.ShowAsync();

view raw
hosted with ❤ by GitHub

Lo and behold, our message is shown!

Message from configuration
Message from configuration

If you are using a MVVM framework with dependency injection, the best way to use configuration is to extract an interface from the AppConfig class and inject the instance as a singleton into your view models instead of instantiating it manually.

Bonus tip

A great thing about the modern configuration infrastructure is that we could also have a appsettings.production.json file, which would not be checked-in the source control and we could include it in our project only for Release builds (by making it conditional in csproj):

<ItemGroup Condition="'$(Configuration)' == 'Release'">
<Content Include="appsettings.production.json">

The pipeline would then be configured as follows:

var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile("appsettings.production.json", optional: true);

Unfortunately, UWP projects can’t use the new SDK-style csproj format yet and Visual Studio has issues properly showing/hiding the conditional file in Solution Explorer for the classic csproj format. Even though this is inconvenient, it still does work as intended!

Source code

Example source code for this article is available on my GitHub.


It is straightforward to include Microsoft.Extensions.Configuration into a UWP project, or in general, any .NET project. We have seen how to use it with JSON configuration files, but it is easily extensible and there are many configuration providers already available for you outside the box in the form of NuGet packages.

Buy me a coffeeBuy me a coffee

2 thoughts on “Using appsettings.json in UWP”

  1. Your solution is both elegant and very helpful. However, I found that installing a side-loaded app (built using the wizard as a Release) through the appinstaller file does not copy the appsettings.production.json to the install folder. Installing via the appxbundle does. I would prefer to use the former. I have found little information that states the reasoning behind this. Do you know why this would be the case? Thanks again for this!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.