.NET MAUI Environment variable
Asked Answered
C

3

7

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.

Crinkly answered 12/9, 2022 at 6:20 Comment(0)
C
2

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.

Crinkly answered 4/9, 2023 at 8:16 Comment(0)
K
3

Well what I usually do for this is I use the if directive

So for instance if I wanna check if wanna set a different value to var in release than in debug I would do something like below :

#if DEBUG
var a = "debug";
#elif RELEASE
var a = "release"
#endif

Good luck hope this helps

Kyd answered 12/9, 2022 at 9:0 Comment(0)
C
2

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.

Crinkly answered 4/9, 2023 at 8:16 Comment(0)
T
0

The only way I've gotten it to work so far is to manually set the variable. In Windows I set a user environment variable ASPNETCORE_ENVIRONMENT=Development and then access it like this

var environment = 
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Update Based on the feedback provided by @Kebechet (thank you!) I ended up doing it like this. Note: I added an option to the the Configuration Manager dropdown.

#if DEBUG
    Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", 
"Development");

#elif TEST
    Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", 
"Test");


#elif RELEASE
    Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", 
"Production");
#endif

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Tamasha answered 2/9, 2023 at 19:20 Comment(1)
So that you dont have to do it manually you can use Environment.SetEnvironmentVariable("name", "value");Crinkly

© 2022 - 2024 — McMap. All rights reserved.