dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json

后端 未结 5 1494
难免孤独
难免孤独 2020-11-29 03:27

When I issue the following command in the command line:

dotnet publish -o \"./../output\" -c Release

The dotnetcli publishes the pr

相关标签:
5条回答
  • 2020-11-29 03:37

    For the new csproj project format you have to add a new ItemGroup with the content

    <ItemGroup>
      <Content Include="appsettings.json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </Content>
      <Content Include="appsettings.Production.json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </Content>
    </ItemGroup>
    

    In case you have multiple appsettings.{env}.json files simply repeat the Content tag inside the same ItemGroup and all your settings files will end up in the publish folder.

    As mentioned in the comments an even cleaner solution is to use a wildcard include:

    <ItemGroup>
      <Content Include="appsettings*json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </Content>
    </ItemGroup>
    

    And all your appsettings files will be published!

    0 讨论(0)
  • 2020-11-29 03:38

    Update: For current (new) .csproj format the CopyToPublishDirectory attribute should be used. It determines whether to copy the file to the publish directory and can have one of the following value:

    • Always,
    • PreserveNewest
    • Never

    So add next section into your .csproj:

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

    Look into @nover answer and SO Exclude or include files on publish for more information about file's control during publishing.


    "In your project.json file you have the section publishOptions with subsection include, where you already have some files like "appsettings.json":

    "publishOptions": {
      "include": [
        "appsettings.json",
        "hosting.json",
        "project.json",
        "web.config"
      ]
    },
    

    You should add "appsettings.Production.json" into this array.

    Updates based on comments:

    • Keep in mind, that all the appsettings.*.json files like appsettings.development.json, appsettings.staging.json and appsettings.production.json will always end up in all environments. You cannot simply handle this using project.json, as it does not support any condition rules. This will be changed in future, when project.json will be replaced back to msbuild and .csproj. If this is critical for your app, consider to use another configuration store, like Environment Variable, database, etc.

    • Note, that order is important, as determine which settings will be applied if they exist in multiple locations. From documentation:

      The order in which configuration sources are specified is important, as this establishes the precedence with which settings will be applied if they exist in multiple locations. In the example below, if the same setting exists in both appsettings.json and in an environment variable, the setting from the environment variable will be the one that is used. The last configuration source specified “wins” if a setting exists in more than one location. The ASP.NET team recommends specifying environment variables last, so that the local environment can override anything set in deployed configuration files.

    0 讨论(0)
  • 2020-11-29 03:40

    After Visual Studio 2017 15.3

    Edit the .csproj file to manually exclude files/folder from being published

    <ItemGroup>
      <Content Remove="appsettings.Development.json" />
    </ItemGroup>
    

    ref: https://www.danielcrabtree.com/blog/273/fixing-the-duplicate-content-error-after-upgrading-visual-studio-2017

    original source

    0 讨论(0)
  • 2020-11-29 03:45

    In your project.json there is a section publishOptions. This lists all the files and folders that will be included when you publish. You will need to update yours to look something like this

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

    You can also use globbing patterns, so you should find this works too (I haven't tested this one)

    {
      "publishOptions": {
        "include": [
          "wwwroot",
          "Views",
          "appsettings*.json",
          "web.config"
        ]
      },
    }
    
    0 讨论(0)
  • 2020-11-29 03:52

    Discovered a three-step build in Visual Studio approach for publishing environment-specific appsetting files (Windows, PowerShell).

    • appsettings.json
    • appsettings.Development.json
    • appsettings.Staging.json
    • appsettings.Production.json

    This approach will publish

    • appsettings.json and
    • appsettings.$(ASPNETCORE_ENVIRONMENT).json.

    Step 1. Update csproj:

      <!-- App Settings -->
      <ItemGroup>
        <Content Remove="appsettings.json" />
        <Content Remove="appsettings.*.json" />
      </ItemGroup>
      <ItemGroup> 
        <Content Include="appsettings.json" CopyToOutputDirectory="Always" />
        <Content Include="appsettings.$(ASPNETCORE_ENVIRONMENT).json" DependentUpon="appsettings.json" CopyToOutputDirectory="Always" />
      </ItemGroup>
    

    Step 2. Set an environment variable in PowerShell:

      # Read
      [Environment]::GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "User")
      # Output: empty string if not set or 'Staging' in my case
      # Set environment variable "User" or "Machine" level
      [Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Staging", "User")
    

    Step 3. Then close and reopen Visual Studio solution to enable Visual Studio to see the environment variable and reload project structure accordingly.

    • Now appsettings.json is a parent and appsettings.Staging.json is a nested file.
    • If you set another environment (for example "Production") and then close and Visual Studio and reopen your solution, then you will appsettings.json as a parent and appsettings.Production.json as a nested file.

    Final step. Run publishing.

    Note: publishing profile environment variables do not affect publishing configuration. This approach uses PowerShell to set an environment variable and enables environment-specific publishing. Please see link for more details on environment variables.

    0 讨论(0)
提交回复
热议问题