简体   繁体   中英

Switching/Setting Environment in ASP.NET Core

I have an asp.net core 1.1.0 website working really well as long as it is running on my machine. It even builds and creates the published website via a TFS build agent. The artifacts are correctly picked up by the release manager and copied to the remote "DEV" server where a new apppool/website is created. It even starts without any issues. The server environment variables are used as the store for the ASPNETCORE_ENVIRONMENT (DEV) and connection strings and are also setup correctly for that environment thanks to the release manager.

My issue is that when the site starts (via IIS) the output log says the hosting environment is "Production". Log output:

Hosting environment: Production
Content root path: D:\Apps\FreightRates
Now listening on: http://localhost:20977
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://auqldrv00dev1si:10000/  
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
      Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Jbssa.FreightRates.Web.Controllers.HomeController.Index (Jbssa.FreightRates.Web) in 66.0757ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 812.9611ms 401 text/plain
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://auqldrv00dev1si:10000/  
info: Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory[1]
      Executed DbCommand (103ms) [Parameters=[@__name_0='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SELECT [u].[ClaimType], [u].[Value]
      FROM [scoControl].[UserClaim] AS [u]
      WHERE [u].[User] = @__name_0
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
      Authorization was successful for user: <mydomainname>.
info: History[0]
      Refreshing Dashboard

Then it falls over because it is attempting to read data from my localdb instance instead of the configured environment variable connection string.

My startup.cs class Startup constructor that reads the configuration details looks like this:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }

    builder.AddEnvironmentVariables();
    var connectionStringConfig = builder.Build();

    var config = new ConfigurationBuilder()
        .AddUserConfiguration(
            options => options.UseSqlServer(connectionStringConfig.GetConnectionString("FreightRatesConnection")));

    foreach (var source in config.Sources)
        builder.Add(source);

    Configuration = builder.Build();
}

That all works fine in "Development", out local pc environment, but doesn't appear to load the environment variables on the web server as expected.

Can anyone see anything obvious as to why the environments are not switching when deployed. By the way, we use the following environments:

  • Development : Local PC/Local database
  • DEV : Development server running IIS/Dev database
  • UAT : QA/UAT server running IIS/UAT database
  • STG : Production mirrors (multiple servers at various locations)
  • PRD : Actual production

There are a few others that can be added to the mix depending on the complexity and risk to users. We don't use the terms "Staging, Production" that ships as defaults in Asp.Net Core. We renamed LCL to Development for simplicity.

Update I am running the AppPool as myself so it isn't that the user can't see the machine environment variable that hold the connection strings. I have also verified that they are set and correct after the Release tasks are run. I have also manually stopped and started the appappol and website "just in case" Though those tasks are also performed by the release tasks.

I had to set the LoadUserProfile on the ApplicationPool in IIS. I was a little surprised I had to do this for machine environment variables.

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