.Net Maui | App Configuration Settings | All Environment (Dev | Prod )

Опубликовано: 15 Июль 2023
на канале: DotNet Real world example
2,069
16

.Net Maui | App Configuration Settings | All Environment (Dev | Prod )

.Net Maui | App Configuration Settings
   • .Net Maui | App Configuration Setting...  

.Net Maui | Add Form | Add Operation
   • .Net Maui | Add Form | Add Operation  

.Net Maui | Consume Api
   • .Net Maui | Consume Api  

.Net Maui | URI base Navigation with model | Binding Listdetails page
   • .Net Maui | URI base Navigation with ...  

.Net Maui | URI base Navigation with parameter | Binding Listdetails page
   • .Net Maui | URI base Navigation with ...  

.Net Maui | List Binding | MVVM advance feature
   • .Net Maui | List Binding | MVVM advan...  

list binding github repo
https://github.com/kartik786-git/dotn...

.net maui | .net multi platform app | window | android | IOS
   • .net maui | .net multi platform app |...  

android emulator setup
   • android emulator setup  

Windows Subsystem for Android| Winodows 11 | .Net Maui App
   • Windows Subsystem for Android| Winodo...  

.Net Maui
App Configuration Settings
All Environment development and proudction
create appsettings.json file
create appsettings-development.json file
create appsettings-production.json file
set property of appsettings.json file
Build Action - Embedded resource
Copy to Output Directory - Copy if newer
add packages
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.Json
Implemention in MauiProgram.cs file to read appsettings.json
Inject Iconfiguration in ViewModel Class
now you can get appsettings value direclly by key or
create class and GetRequiredSection we able to get all key and value.

.NET MAUI is right around the corner and Preview 13 is out right now! There are so many great features packed into .NET MAUI and my favorite has to be all of the great Dependency Injection and configuration that goes into the MauiProgram startup.

But, what if you want to do other things that aren't included out of the box... such as reading app configuration through a appsettings.json file?!?!? Well you can, and it is super easy! Thanks to the MauiAppBuilder we can use the ConfigurationManager that is built in to configure settings in our .NET MAUI app. First things first, let's add the appsettings.json file as an EmbeddedResource:

Next, let's create a few classes that represent these configuration settings anywhere in the project:

We will now need to configure our json file with a few Microsoft.Extensions.Configuration NuGet packages. Added the following into your .csproj:

Now, let's read in our json file, and add it to our builder.Configuration in our CreateMauiApp method after we create the builder:

he code above reads in the manifest resource from the assembly into a stream. You will want to change the MauiApp to whatever your assembly name is for your project. After that we load it up and add our new configuration to the builder's main ConfigurationManager. We do this so we can dependency inject our IConfiguration anywhere we want.

Note here that there will be some perf here for reading it in from the file. You should consider if you really need to use appsettings.json as your json is never going to change once compiled up.

viewmodel file

first approch to get
string apiBaseUri = _configuration["ApiUri"];

second approch
ApiConfigurationSetting apiConfigurationSetting = _configuration.GetRequiredSection("ApiSettings")


mauiprogramme.cs files

var builder = MauiApp.CreateBuilder();

string appsettingfile = "dotnetmauiallfeaturedemo.appsettings.json";

var getAssemebly = Assembly.GetExecutingAssembly();

using var stream = getAssemebly.GetManifestResourceStream(appsettingfile);


var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();

builder.Configuration.AddConfiguration(config);

appsettings.json file

{
"ApiUri": "https://learningtechskills.com/",
"ApiSettings": {
"ApiUri": "https://learningtechskills.com/",
"Testuri": "debug mode environment"
}
}

appsettings-development.json file

{
"ApiUri": "https://learningtechskills.com/",
"ApiSettings": {
"ApiUri": "https://learningtechskills.com/",
"Testuri": "development"
}
}

appsettings-production.json file

{
"ApiUri": "https://learningtechskills.com/",
"ApiSettings": {
"ApiUri": "https://learningtechskills.com/",
"Testuri": "production"
}
}


Смотрите видео .Net Maui | App Configuration Settings | All Environment (Dev | Prod ) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь DotNet Real world example 15 Июль 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,069 раз и оно понравилось 16 людям.