.NET Blazor MAUI Hybrid App - Add appsettings.json

206 Views Asked by At

I am currently creating an application for diving into .NET Blazor MAUI Hybrid App Development using .NET 8 in Visual Studio 2022 version 17.9.2

I now want to add a appsettings.json file to the project.

So i created appsettings.json directly in the wwwroot folder:

appsettings in wwwroot

Then i had two attempts to add it to the MauiProgram.cs to make it available for dependency injection.

Attempt 1


public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>();


        // Load appsettings.json
        var configurationBuilder = new ConfigurationBuilder()
            .AddJsonFile("wwwroot/appsettings.json", optional: false, reloadOnChange: true);

        IConfiguration configuration = configurationBuilder.Build();
        builder.Configuration.AddConfiguration(configuration);

        builder.Services.AddMauiBlazorWebView();

        builder.Services.AddRadzenComponents();

        
        ConfigureServices(builder.Services);
#if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }

    static void ConfigureServices(IServiceCollection services)
    {
        //services.AddScoped<XyzService>();
    }
}

Attempt 2

appsettings have to be embedded resource (rightclick on appsettings.json > properties > Build Action > embedded resource)


public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>();

        // Load appsettings.json
        using var appsettingsStream = Assembly
            .GetExecutingAssembly()
            .GetManifestResourceStream("MyApp.wwwroot.appsettings.json");

        if (appsettingsStream != null)
        {
            var config = new ConfigurationBuilder()
                .AddJsonStream(appsettingsStream)
                .Build();

            builder.Configuration.AddConfiguration(config);
        }

        // Just an example on how to get a string from appsettings
        // var appName = builder.Configuration.GetValue<string>("ApplicationSettings:AppName");

        builder.Services.AddMauiBlazorWebView();

        builder.Services.AddRadzenComponents();

        builder.Services.ConfigureServices();
#if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }

    /// <summary>
    /// Extension method for configuring custom services
    /// </summary>
    /// <param name="services"></param>
    static void ConfigureServices(this IServiceCollection services)
    {
        //services.AddScoped<XyzService>();
    }
}


So Attempt 1 was successfully creating a windows app but was failing when i was trying to build an Android App (Simulator) with the error

The configuration file 'wwwroot/appsettings.json' was not found and is not optional. The expected physical path was '/data/user/0/com.companyname.myappname/files/wwwroot/

Attempt 2 seems to work but i am not sure if thats the way how it should be done?

I am glad for every help or advice

1

There are 1 best solutions below

0
Guangyu Bai - MSFT On BEST ANSWER

The second method is right. You need to set the appsettings to embedded resource. Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.