How to get the Development/Staging/production Hosting Environment in ConfigureServices
You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices.
public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv)
{
...your code here...
CurrentEnvironment = env;
}
private IWebHostEnvironment CurrentEnvironment{ get; set; }
public void ConfigureServices(IServiceCollection services)
{
string envName = CurrentEnvironment.EnvironmentName;
... your code here...
}
Accessing the IHostingEnvironment in ConfigureServices method
just create a property in the Startup class to persist the IHostingEnvironment. Set the property in the Startup constructor where you already have access, then you can access the property from ConfigureServices
Access the current environment (development, production, etc.) from within the builder setup in .NET 6
You can use Environment
property on WebApplicationBuilder
:
var isDev = builder.Environment.IsDevelopment();
How to get environment variables from inside Main() in ASP.NET Core?
You haven't quite explained what it is you are trying to get (the environment variables or the environment object). But based on some of the code in the question it appears like you want access to the IWebHostEnvironment
.
That's easy. IHost
has a Services
property. You just need to request the appropriate service.
IHost host = CreateHostBuilder(args).Build();
IWebHostEnvironment env = host.Services.GetRequiredService<IWebHostEnvironment>();
// now do something with env
SeedDatabase(host);
host.Run();
Now if you need the environment prior to everything else, you can also access the environment from the WebHostBuilderContext
inside of a different overload of ConfigureServices
:
webBuilder.ConfigureServices((ctx, services) => {
IWebHostEnvironment env = ctx.HostingEnvironment;
// Do something with env
});
Similarly ConfigureAppConfiguration
, ConfigureHostConfiguration
and ConfigureServices
on the IHostBuilder
(the generic host) also provide access to the IHostEnvironment
(note lack of "Web"):
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, config) => {
IHostEnvironment env = ctx.HostingEnvironment;
})
.ConfigureServices((ctx, services) => {
IHostEnvironment env = ctx.HostingEnvironment;
})
.ConfigureWebHostDefaults(/* etc */ );
The same service collection, environments etc are used by the generic host and the web host (in addition to the added IWebHostEnvironment
)
Side note: Usually Startup.cs isn't invoked until the first call is made to your application. That is probably why it appears to not be running.
ASP.NET Core How to set variables for Dev, Staging and Production environments
You can set environment variable in web.config with this manual like this:
<aspNetCore processPath="dotnet"
arguments=".\MyApp.dll"
stdoutLogEnabled="false"
stdoutLogFile="\\?\%home%\LogFiles\stdout"
hostingModel="InProcess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="CONFIG_DIR" value="f:\application_config" />
</environmentVariables>
</aspNetCore>
Unable to get the Environment in .Net Core Application
The space in the "DOTNET_ENVIRONMENT " is causing the issue.
Related Topics
Algorithm for Intersection of 2 Lines
Methodinfo.Invoke with Out Parameter
How am I Misusing the Null-Coalescing Operator? Is This Evaluating "Null" Correctly
How to Move Rigidbody Gameobject
How to Sort Depended Objects by Dependency
Improve Wpf Datagrid Performance
Remove the Title Bar in Windows Forms
"Movefile" Function in C# (Delete File After Reboot)
Yield Return Statement Inside a Using() { } Block Disposes Before Executing
Visual Studio 2010 Conditional References
How to Compile a C# File with Roslyn Programmatically
How to Extract Subscript/Superscript Properly from a PDF Using Itextsharp
How to Return the Current Action in an ASP.NET MVC View
Return Content with Ihttpactionresult for Non-Ok Response
How to Get Xml Node from Xdocument