Sdk: appsettings files aren't included in publish folder when using PublishSingleFile

Created on 26 Jul 2019  ·  6Comments  ·  Source: dotnet/sdk

I'm testing out the /p:PublishSingleFile=true option and it's working well so far, but my appsettings.json doesn't get included in the publish folder. If I don't use PublishSingleFile=true then the file does get copied to the publish folder.

I'm using the following to publish:

dotnet publish -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true

In my .csproj I have the following but it doesn't seem to have an effect when PublishSingleFile=true:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <None Update="appsettings*.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
  </ItemGroup>

</Project>

I'm using SDK version 3.0.100-preview7-012821.

Current workaround

After looking through some GitHub issues I found mention of ExcludeFromSingleFile. If I add that to my appsettings entry then the file gets included in the single file publish results.

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <None Update="appsettings*.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" ExcludeFromSingleFile="True" />
  </ItemGroup>

</Project>

Since these files are the standard way of handling settings in a file I'd assume they would be included in the publish results without any extra settings, or at least if CopyToPublishDirectory is set.

All 6 comments

@nguerrera @fadimounir can you take a look at this?

cc @swaroop-sridhar

@xt0rted I think the json file does get included, but not in the publish folder, rather in the bundled single file (your large app.exe file after publishing). @swaroop-sridhar can confirm and help out with single file issues

@xt0rted:

CopyToPublishDirectory setting determines the set of files to be published (the name is a bit misleading in the context of SingleFile publish.

Of these files, only those marked ExcludeFromSingleFile are left behind in the publish directory, and the rest are bundled into the single-file app.

When the single-file app is run, the settings file is extracted next to the app.dll (in appcontext.BaseDirectory) which is likely where the settings file is expected by the app.

So, this behavior is by design.

The functionality is a bit confusing then. How would you bundle the settings files so they're editable? I'm storing api settings that can, and will, change after deployment so I need to be able to edit what's in the appsettings.json whenever these change.

This console app is being installed as a windows service and I'll have multiple copies running that have different config settings. Even if I had a database backing these I'd still need someway to adjust the connection string being used, which is also stored in appsettings.json.

The "workaround” looks fine and supported for that.

@xt0rted the publish settings provide the building blocks to either include or exclude a file from the single-file. In this case, ExcludeFromSingleFile=true is the correct setting for appsettings.json. I only meant to say that there may be additional work on behalf of the app (in general) due to the fact that the file is next to the single-file and not the app.dll. Thanks.

Was this page helpful?
0 / 5 - 0 ratings