简体   繁体   English

appsettings.Production.json具有来自appsettings.json之后的值

[英]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 我在appsettings.json和appsettings.Production.json中具有不同的连接字符串,但是在发布到计算机上的IIS文件夹后,我在appsettings.Production.json中具有与appsettings.json中相同的连接字符串。

My Program.cs has 我的Program.cs有

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 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 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: 我有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 和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: 发布后,我在IIS文件夹中有来自appsettings.json的值的appsettings.Production.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. 我想要像我在Visual Studio中的项目一样具有appsettings.Production.json。 Why my appsettings.Production.json is overriden by appsetting.json? 为什么我的appsettings.Production.json被appsetting.json覆盖?

I've figure out that appsettings.Production.json takes values from CustomProfile.pubxml.user 我发现appsettings.Production.json从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. 在这里,我有与appsetting.json中一样的连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM