How does IHostingEnvironment.EnvironmentName work?

In a .NET Core console application, if I add the following line ...

IHostingEnvironment env = new HostingEnvironment(); Console.WriteLine(env.EnvironmentName); 

I get this result ...

Products

But when I do the same in an ASP.NET Core application on the same machine ...

 public Startup(IHostingEnvironment env) { Debug.WriteLine(env.EnvironmentName); } 

I get this result ...

Development

  • How does EnvironmentName work?
  • How can I indicate that my local machine is a development environment?
  • How can I indicate that Azure is a production environment?

As an additional point, is it possible to configure EnvironmentName to work with Debug and Release configurations within the solution?

enter image description here

  • Debugging => Development
  • Release => Production

The ultimate goal is the ability to connect to a local SQL database when building using Debugging and the Azure database when building using Release .

+5
source share
1 answer

How does EnvironmentName work?

.NET Core reads the name from an environment variable .

How can I indicate that my local machine is a development environment?

Set the Development environment variable.

How can I indicate that Azure is a production environment?

Set the environment variable Production .

As an additional point, is it possible to configure EnvironmentName to work with Debug and Release configurations within the solution?

You can create a launch profile that sets the environment name and uses this profile with Debug or Release settings. The startup profile affects EnvironmentName when launched from Visual Studio; You will need to use other means to install it when you launch the application in other environments.

The following figure shows how to run the application in the Release configuration and the MyDevProfile file, which sets EnvironmentName to Development .

enter image description here

+5
source

Source: https://habr.com/ru/post/1259525/


All Articles