-1

I have a Blazor Server Side application.

In the program.cs I have a check if the application is running in development, production or staging.

if (builder.Environment.IsDevelopment())
{
    Console.WriteLine("##### Running in Development ####");
}
if (builder.Environment.IsProduction())
{
    Console.WriteLine("##### Running in Production ####");
}
if (builder.Environment.IsStaging())
{
    Console.WriteLine("##### Running in Staging ####");
}

I want to display this in my MainLayout.razor.

How would I approach this? Can I parse this somehow or can I access the builder somehow through a service?

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
SideSky
  • 313
  • 3
  • 15
  • 1
    `"I want to display this in my MainLayout.razor"` Did you mean you want to display this status in layout? – Md Farid Uddin Kiron Apr 20 '23 at 06:48
  • Does this answer your question? [How do I access env.IsDevelopment() from a Blazor page?](https://stackoverflow.com/questions/57814637/how-do-i-access-env-isdevelopment-from-a-blazor-page). You can check the environment directly inside `MainLayout.razor` by injecting `IWebHostEnvironment`. – Dimitris Maragkos Apr 20 '23 at 08:05
  • Conditionally rendering using the current hosting environment, you can use the [Environment Tag Helper](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/environment-tag-helper?view=aspnetcore-7.0) attribute. You can pass a comma-separated list of environment names. If any of the provided environment names match the current environment, the enclosed content is rendered. This can be used in **_host.cshtml**. – Suprabhat Biswal Apr 20 '23 at 08:42

3 Answers3

2

I would use preprocessor directives:

#if DEBUG
    Console.WriteLine("Debug version");
#else
    Console.Writeline("Prod");
#endif

You can slap this pretty much anywhere in your program. Specifically in your case of a blazor app, not sure what you want to do in those conditions, but for instance you could have one component or text block show up in dev, and a different component in release.

One advantage of using the preprocessor directives is that you don't end up with all the unnecessary conditionals and extra lines of non-applicable code in production. Your release version of the code will only have what's under the else statement above.

One tip I have though is that you should change your build mode to "release" and make sure it compiles with your "else" conditions before committing or doing pull requests. I've made this mistake where you end up only compiling the Debug version of code, everything checks out, but the compile-time warnings won't show up for the other condition unless you build in that mode.

Here's a link where you can read all the gory details about how the directives work: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

The pros/cons of using proprocessor directives vs the environment check can be found here, amongst other places. #if DEBUG vs if (env.IsDevelopment())

Edit: also wanted to add the maybe non-obvious tip that you can define your own constants as arguments in the msbuild string, for example.

/p:DefineConstants="STAGING;OTHERSTUFF"

In this way, you can expand your preprocessor directive inquiries beyond the stock constants, which may be more useful as you build and deploy to each environment. For instance, if you build release versions for both stage and prod, defining your own constants for the build/deploy process such as within your devops ci/cd pipelines can give you finer grained control over the directives.

user71030
  • 106
  • 5
1

How would I approach this? Can I parse this somehow or can I access the builder somehow through a service?

Actually. if you you would like to display your application environment status, in that case it can be achieved in numerous way. Either you can create a static class and then pass the value and then can access anywhere within your application or create extension method or even directly access EnvironmentVariable

Let's have a look in practice:

Using Static Class;

public static class GetEnvironmntInfo
{
    public static string? Info { get; set; }
}

Bind environment info in static property:

if (builder.Environment.IsProduction())
{
    GetEnvironmntInfo.Info = "#### Running In Production ####";
}
if (builder.Environment.IsDevelopment())
{
    GetEnvironmntInfo.Info = "#### Running In Development ####";
}
if (builder.Environment.IsStaging())
{
    GetEnvironmntInfo.Info = "#### Running In Staging ####";
}

Access in MainLayout.razor:

     <div>
        <h1>Way:1</h1>
        @{
           <strong>@GetEnvironmntInfo.Info</strong>
        }
    </div>

Output:

enter image description here

In addition to this, alternatively you can access ASPNETCORE_ENVIRONMENT info from anywhere by using following approach:

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

Now, you can set your conditional as well.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
0

I created a EnvironmentService:

public class EnvironmentService
{
    private readonly IWebHostEnvironment _environment;

    public EnvironmentService(IWebHostEnvironment environment)
    {
        _environment = environment;
    }

    public string GetEnvironment()
    {
        if (_environment.IsDevelopment())
        {
            return "Development";
        }
        else if (_environment.IsProduction())
        {
            return "Production";
        }
        else if (_environment.IsStaging())
        {
            return "Staging";
        }
        else
        {
            return "Unknown";
        }
    }
}

Added it to my program.cs:

builder.Services.AddScoped<EnvironmentService>();

And could use it in my MainLayout.razor with injecting it:

@inject EnvironmentService EnvironmentService

<p>@EnvironmentService.GetEnvironment()</p>
SideSky
  • 313
  • 3
  • 15
  • 1
    As said through numerous way it can be achieved. Interestingly, your approach also working the same mannere behind the scene. – Md Farid Uddin Kiron Apr 20 '23 at 07:30
  • 1
    Or you could directly inject `IWebHostEnvironment` in your razor component. [How do I access env.IsDevelopment() from a Blazor page?](https://stackoverflow.com/questions/57814637/how-do-i-access-env-isdevelopment-from-a-blazor-page) – Dimitris Maragkos Apr 20 '23 at 08:57