How to use appsettings.json with Xamarin.Forms
There are two key differences compared to ASP.NET:
- first, we’ll be working with an Embeded Resource as opposed to a file on the disk
- second – we will register the
appsettings.json
file ourselves
To get started, add an appsettings.json
file in your shared project. Make sure you set it’s Build Action
to Embeded Resource
. Add some keys + value in the file that we can use for testing. For example:
{
"ChatHubUrl": "https://signalrchatweb.azurewebsites.net/"
}
Code language: JSON / JSON with Comments (json)
Next, we need to get hold of the resource stream.
Stream resourceStream = GetType().GetTypeInfo().Assembly.GetManifestResourceStream("SignalRChat.appsettings.json");
Code language: C# (cs)
And use it to build an IConfiguration
object.
var configuration = new ConfigurationBuilder()
.AddJsonStream(resourceStream)
.Build();
Code language: C# (cs)
Now, in order to pull the configuration values from it, just use it like you would any other dictionary.
configuration["ChatHubUrl"];
Code language: C# (cs)
Alternatively you can register it into your IoC container as an IConfiguration
, inject that into your viewmodels and use it in the same way.
For a complete example you can check out this repository on GitHub: Xamarin Forms – SignalR Chat.