When I issue the following command in the command line:
dotnet publish -o "./../output" -c Release
The dotnetcli
publishes the project correctly. However, it does not copy the appsettings.Production.json
file, only the appsettings.json
.
Why is this? I have googled around and read the official core docs, but haven't found how the correct environment appsettings.json
is supposed to end up in the publish output.
Should I copy appsettings.Production.json
manually to the published folder?
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 likeappsettings.development.json
,appsettings.staging.json
andappsettings.production.json
will always end up in all environments. You cannot simply handle this usingproject.json
, as it does not support any condition rules. This will be changed in future, whenproject.json
will be replaced back tomsbuild
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.
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"
]
},
}
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!
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>
来源:https://stackoverflow.com/questions/37858312/dotnet-publish-doesn%c2%b4t-publish-correct-appsettings-env-environmentname-json