简体   繁体   中英

.NET Core 3.1 Environment not correct

I have a .NET Core 3.1 Web API I recently pushed to a dev Azure VM to run on IIS. Upon deploying the API, I noticed the app wasn't starting up and I wasn't even getting a detailed IIS stack trace, just a plain 404 error. So I checked the Windows Event Viewer and found the following:

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)

The connectionString key only existed in my appsettings.Development.json. It dawned on me to add the connection string to my appsettings.json and the error went away. But I wanted to use a different db connection string for my dev environment. So I know what you all are thinking at this point, so I went ahead and checked/confirmed the environment variables on the server:

环境

C:\windows\system32>reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    ASPNETCORE_ENVIRONMENT    REG_SZ    Development
    ComSpec    REG_EXPAND_SZ    %SystemRoot%\system32\cmd.exe

So I went ahead and added the following to my Startup:

       public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            var connstr = Configuration.GetConnectionString("MyDbContext");

            logger.LogInformation($"ApplicationName: {env.ApplicationName}");
            logger.LogInformation($"EnvironmentName: {env.EnvironmentName}");
            logger.LogInformation($"ContentRootPath: {env.ContentRootPath}");
            logger.LogInformation($"WebRootPath: {env.WebRootPath}");
            logger.LogInformation($"DbConnection: {connstr}");

and checked my stdout logs:

info: MyProject.API.Startup[0]
      ApplicationName: MyProject.API
info: MyProject.API.Startup[0]
      EnvironmentName: Production
info: MyProject.API.Startup[0]
      ContentRootPath: C:\inetpub\wwwroot\myapi
info: MyProject.API.Startup[0]
      WebRootPath: 
info: MyProject.API.Startup[0]
      DbConnection: <my connection string from appsettings.json>

So I don't understand if this is some new .NET Core 3.1 change or why my API is not detecting the ASPNETCORE_ENVIRONMENT variable as Development.

Only desktop programs pick up system environment variable changes without a reboot.

Processes inherit environment variables from their parent processes, and only Explorer (the shell) listens for changes to the registry and reloads its environment variables when there are changes to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment . Then any new process launched from Explorer will inherit the changed environment variables.

Services (like IIS) do not, and even IISRESET or recycling App Pools will not help. You must reboot.

The algorithm ASP.NET Core is using is described in Use multiple environments in ASP.NET Core :

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  1. DOTNET_ENVIRONMENT
  2. ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults . The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT .

IHostEnvironment.EnvironmentName can be set to any value, but the following values are provided by the framework:

  • Development : The launchSettings.json file sets ASPNETCORE_ENVIRONMENT to Development on the local machine.
  • Staging
  • Production : The default if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.

Also make sure to reload the IIS process, once you set the environment variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM