Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set production server to production

On my production server I have set environment variable by adding the following to /etc/environment :

 ASPNETCORE_ENVIRONMENT=Production

I checked it has recorded with printenv ASPNETCORE_ENVIRONMENT after a reboot.

My server is Ubuntu 14.04 and I am using asp.net core 1.1.

It is loading my appsettings.Development.json instead of appsettings.Production.json.

This is my startup.cs contructor

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

Inside my log file I can correctly see it says Hosting environment: Production but if I output values from configuration file to view then it is values from contents of appsettings.Development.json.

I even tried deleting appsettings.Development.json from server and rebooting entire server but it still pulls the same values I guess it must be compiled somewhere.

I have also tried adding this to .csproj:

  <ItemGroup>
    <None Include="appsettings.*.json" CopyToPublishDirectory="Always" />
  </ItemGroup>

My settings files appear nested inside VS2017 like this:

enter image description here

The default appsettings.json just has a defualt value for logging, it doesn't contain the values I am pulling.

I can't figure out what the issue is.

like image 925
Guerrilla Avatar asked Oct 18 '22 16:10

Guerrilla


1 Answers

Possibly a long shot, but you may have fallen into the same trap that I did. You need to explicitly include environment-based appsettings.*.json files during the publish process. They were not included by default for me, though I haven't checked the latest project templates to see if that's still true. If you haven't checked whether the appsettings.Production.json file is physically present on your production server, it might be worth a look.

If it turns out that this is your problem and you're still using project.json then if yours looked like mine used to, you'll need to add something like this:

  "publishOptions": {
    "include": [
      "wwwroot",
      "appsettings.json",
      "appsettings.*.json",
      "web.config"
    ]
  }

With the "appsettings.*.json" entry being the important one.

If you've upgraded to csproj then this link may help you work out what you'll need in the new project file format.

like image 156
Polynomial Avatar answered Oct 20 '22 11:10

Polynomial