Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Stage/Production appsettings Asp.net Core

No the guide seems quite clear (to me at least) on how production settings can be put in place. They just talk about how some of this works, but there is no step-by-step guide. I have done the following:

Attempting to change the Environment Variable in Project using this Guide

I get this error despite the Microsoft Doc explicitly stating to reuse ASPNETCORE_ENVIRONMENT. I cannot save these settings

Shows an ! next to ASPNETCORE_ENVIRONMENT

Ignoring this issue, it looks like you can manually create these entries in launchSettings.json so I have:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:18549/",
      "sslPort": 44319
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/v10Staff",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_ENVIRONMENT_T": "Test"
      }
    },
    "IIS Express (Stage)": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/v10Staff",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Stage"
      }
    },
    "ApplicationName": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:60000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_ENVIRONMENT_T": "Test"
      }
    }
  }

Adding Environment Variable to Server

I have added the environment variable according to the documentation I went to Control Panel > System > Advanced system settings. And added the `New User Variable. "ASPNETCORE_ENVIRONMENT" with value "Stage"

Configuration Piece using this Guide

The next issue I have is, no one shows a working "transform" of the appsettings.Stage.json. Does it just completely use the Stage file over the appsettings file? Do I need to just explicitly state what is different? That being said here are my files.

appsettings.Stage.json

{
  "WebServiceAccount": {
    "AccountName": "WebUser",
    "AccountPass": "Wedontcarethatthisishere"
  },
  "ServerLocation": {
    "ServerName": "http://appss.domain.com:8080",
    "DBConnection": "server=svAppNameSTG;Max Pool  Size=6000;database=ES;Integrated Security=SSPI;MultipleActiveResultSets=True"
  }
}

appsettings.json

{
  "WebServiceAccount": {
    "AccountName": "WebUser",
    "AccountPass": "testpass1"
  },
  "ServerLocation": {
    "ServerName": "http://appst.domain.com:8080",
    "DBConnection": "server=svAppNameTST;Max Pool  Size=6000;database=ES;Integrated Security=SSPI;MultipleActiveResultSets=True"
  }
}

I know that the settings, and configuration piece are set up correctly. Because our Test environment works. When I deploy this to our Stage server, I have confirmed that it still points to the Test box.

If anyone has a guide or can spot what is wrong with this that would be great.

For Posterity

here is the Startup builder

var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
like image 700
christopher clark Avatar asked Feb 15 '17 16:02

christopher clark


1 Answers

First of all, you seem to be trying to use the ASPNETCORE_ENVIRONMENT variable multiple times in your first screenshot. You shouldn't be adding new variables, but rather changing the value of the variable to reflect the environment you want to use. So to test Stage locally, simple modify the value of the ASPNETCORE_ENVIRONMENT variable from Development to Stage.

I haven't come across using an ASPNETCORE_ENVIRONMENT_T variable for anything. If this is something you added yourself I suspect it stems from misunderstanding the above and there is probably a better way to achieve what you want. It must be stressed that everything in the launchSettings.json is for your local machine/development environment.


Setting the ASPNETCORE_ENVIRONMENT variable on your Stage or Production servers will differ depending on how you're hosting. IIS? Azure? AWS? Docker? The following answer discusses setting environment variables for IIS:

Publish to IIS, setting Environment Variable


Regarding the appsettings.json, the values are merged. Everything is read from the base file first, and then everything read from the appsettings.{env.EnvironmentName}.json will append or overwrite what is in the base file. In the example you gave, the WebServiceAccount / AccountName variable could be omitted from the appsettings.Stage.json as it is identical to the value in the base.

like image 94
Polynomial Avatar answered Sep 21 '22 07:09

Polynomial