简体   繁体   中英

asp.net core 1 appsettings.production.json not updating connection strings

I had an application in production using .net core RC1 running on IIS. I re-wrote the application because the company wanted it on .net core1 since that is the official release.

In my .net core RC1 application I had config.json and config.production.json and in the production version I simply included the settings I wanted overridden like the connection string. That worked fine.

In .net core1 I have appsettings.json and appsettings.production.json. However, the connection string doesn't seem to be getting updated. It seems to use the localdb from the original appsettings.json.

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-DecisionEff-b32ae861-2422-4f88-839c-2a6d599fee45;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

appsettings.production.json:

{
    "ConnectionStrings": {
      "DefaultConnection": "Server=mydb.server;Database=DbName;User ID=User;Password=password;Trusted_Connection=False; Connection Timeout= 30;"
    }
}

In my startup file I do have it set to look for production:

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

The above was modeled after what I was doing in RC1 that worked. Was there something else that changed so connections strings are not udpated? If I put the production db connection string in the appsettings.json file then the application works fine on our production server. So it is not pulling the setting from the appsettings.production.json file. I know it is reading the environment properly because I am printing it out and it is as expected.

How do you provide different development and production db settings in .net core 1 web app?

Any help would be appreciated. I am obviously overlooking something.

UPDATE: When I run this on my local machine setting ASPNETCORE_ENVIRONMENT to Production it uses the proper connection string. So the web app is seeing the proper environment on production and can use the connection string. Is there something that has to be configured during build to send the different environment settings? Or is there a way it can print the different settings from the .json files for the environments?

UPDATE 2: I noticed that when I publish the application the there is only appsettings.json which has the localdb connection only. There doesn't seem to be any appsettings.production.json file generated. Is there somewhere you have to specify during the build now that these different settings should come across?

You probably forgot to set the environment variable that sets the environment. You have to set ASPNETCORE_ENVIRONMENT to Production

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