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.
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.
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.
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
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.
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");
© 2022 - 2024 — McMap. All rights reserved.
Environment.SetEnvironmentVariable("name", "value");
– Crinkly