Search code examples
c#.netmaui.net-mauimaui-blazor

.NET MAUI Environment variable


Is there a way how to find out that the current environment in .NET MAUI is development ? Because before in my Blazor WASM I did just: builder.HostEnvironment.IsDevelopment()

But in MauiAppBuilder I dont see any environment property.


Solution

  • I approached it a little bit different. I have created MobileHostEnvironment:

    internal sealed class MobileHostEnvironment : IHostEnvironment
    {
        public string EnvironmentName { get; set; } = Environments.Development;
        public string ApplicationName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string ContentRootPath { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public IFileProvider ContentRootFileProvider { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }
    

    then at the beginning of MauiProgram.cs I declare and initialize it like:

    private static readonly MobileHostEnvironment _mobileHostEnvironment = new()
    {
        EnvironmentName = Environments.Production
    };
    

    with a reference to using Microsoft.Extensions.Hosting; and before service registering I have:

    #if DEBUG
            _mobileHostEnvironment.EnvironmentName = Environments.Development;
    #endif
    

    So that way app in debug is always Development and released one is always Production.

    Ofc you can improve this flow by registering it into dependency container.