简体   繁体   中英

appsettings.Production.json has values from appsettings.json afte publishing

I have different connection strings in appsettings.json and appsettings.Production.json, but after Publishing to IIS folder on my computer I have the connection strings in appsettings.Production.json same as in appsettings.json

My Program.cs has

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>            
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;

                    using (var f = System.IO.File.AppendText(@"C:\temp\log.txt"))
                    {
                        f.WriteLine(env.EnvironmentName);//for Debug, it outputs "Production" (without quotes)
                    }

                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
                })
                .UseStartup<Startup>()
                .UseNLog();

appsettings.json

{
//something is omitted
  "ConnectionStrings": {
    "Northwind": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

app.Production.json

{
  "ConnectionStrings": {
    "Northwind": "\"Data Source=(localdb)\\\\mssqllocaldb;Initial Catalog=Northwind;User Id=sa;Password=Password123",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;User Id=sa;Password=Password123;MultipleActiveResultSets=true"
  }
}

I have CustomProfile.pubxml:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://olegtarusov.com</SiteUrlToLaunchAfterPublish>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <ProjectGuid>917b0f52-135a-4943-991f-12f35fa4a9c6</ProjectGuid>
    <SelfContained>false</SelfContained>
    <_IsPortable>true</_IsPortable>
    <MSDeployServiceURL>localhost</MSDeployServiceURL>
    <DeployIisAppPath>OlegTarusov</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>InProc</MSDeployPublishMethod>
    <EnableMSDeployBackup>False</EnableMSDeployBackup>
    <UserName />
    <_SavePWD>False</_SavePWD>
  </PropertyGroup>
</Project>

and launchSetting.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50806",
      "sslPort": 44348
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Oleg_Tarusov": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

After publishing I have appsettings.Production.json in IIS folder with values from appsettings.json:

{
  "ConnectionStrings": {
    "Northwind": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True",
    "ApplicationIdentityDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=OlegTarusovIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

I want to have appsettings.Production.json as in my project in Visual Studio. Why my appsettings.Production.json is overriden by appsetting.json?

I've figure out that appsettings.Production.json takes values from CustomProfile.pubxml.user

On the second step of configuration a profile ("Publish..". in the context menu) you can set up connection strings for databases. Here I had the connection strings as in appsetting.json.

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