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:

https://gist.github.com/MartinZikmund/f5e66e49a1532e35b26155c6a9e93587

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

https://gist.github.com/MartinZikmund/5b42ac35c9a4e5f8cc1ad8eef205ac6b

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:

https://gist.github.com/MartinZikmund/0d2a3c94eeec2b9fa07dc3921bc4ba95

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

https://gist.github.com/MartinZikmund/0b90ab3abd4a87efee41d1194493634b

And create a property to retrieve this section:

https://gist.github.com/MartinZikmund/1915bde780a735d57205fd69c3b338e9

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

https://gist.github.com/MartinZikmund/4735c24a7f998cacad79ac43ccde15e7

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):

https://gist.github.com/MartinZikmund/9a3d579d297016eee72f44de46bf20c2

The pipeline would then be configured as follows:

https://gist.github.com/MartinZikmund/b929bf7763007ea221a2ffc8671f66b9

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.

Summary

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

5 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!

  2. Hello Martin,
    Thank you for this nice, simple approach for implementing app settings. I was able to use this in my WPF(.NetCore) app.

  3. Hello, thanks for your post. It helped me a lot.
    I want the user to set the settings in my app. Is there a “best practice” way to serialize and save them back to appsettings.json?

Leave a Reply to Paul Cancel 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.