4

I have a new .NET Core 3.1 worker class that is hosted as a Windows Service. I am using the default appsettings.json and appsettings.environment.json that were created by the template. The appsettings is loaded from the hostContext during ConfigureServices

.ConfigureServices((hostContext, services) =>
   {
      services.AddHostedService<Worker>();
      services.Configure<BasicSettings>(hostContext.Configuration.GetSection("AppSettings"));
   });

I want to be able to edit the appsettings after it is deployed so that I can change settings in production. It works correctly during debugging on my machine. I updated the csproj file to have the following code to try and make the appsettings.json not get included in the Single file.

    <None Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
    <None Include="appsettings.Development.json;appsettings.Production.json;">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
      <DependentUpon>appsettings.json</DependentUpon>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
  </ItemGroup>

After adding this the publish process does create the single exe as well as the 3 appsettings.json files but does not solve it.

When the windows service starts up it expands the single exe to the folder C:\Users\ServiceLogonUser\AppData\Local\Temp.net\ServiceName\SomeRandomThing and this has the appsettings.json that exists in the project at publish. Not the appsettings.json that is copied next to the exe. If I delete this folder, it is recreated but again with the appsettings.json that existed at publish. How with a single exe publish can it read the appsettings.json from the same folder so that the file can be edited in after publish?

Nick Gelotte
  • 307
  • 4
  • 10
  • You're supposed to modify `appsettings.Production.json`, not `appsettings.json`. *DON'T* delete that folder. The `.exe` is just a package that's deployed to that folder if it doesn't exist. – Panagiotis Kanavos Dec 17 '19 at 11:25
  • The only file that needs to be deployed alongside the `.exe` is `appsettings.Production.json`. `Development` contains your *development* settings and shouldn't be published – Panagiotis Kanavos Dec 17 '19 at 11:26
  • The problem is that the appsettings.Production.json that is next to the exe does not get copied to the Temp folder. So the only way I can make changes in production is to edit the json file in the temp folder. But if I launch a new version it creates a new temp folder again with the json files that existed in the project at the time I published - I would have to go find the new temp folder and manually edit or copy over the json every time I publish a version. – Nick Gelotte Dec 17 '19 at 17:33

1 Answers1

4

I too faced the same problem and solved with this simple change in the project file.

<None Include="appsettings.json">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  <CopyToPublishDirectory>Always</CopyToPublishDirectory>
  <ExcludeFromSingleFile>false</ExcludeFromSingleFile>
</None>
<None Include="appsettings.Development.json;appsettings.Production.json;">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  <CopyToPublishDirectory>Always</CopyToPublishDirectory>
  <DependentUpon>appsettings.json</DependentUpon>
  <ExcludeFromSingleFile>false</ExcludeFromSingleFile>
</None>

This will bundle the appsettings.json and other configuration JSON files into the Single file and will be unpacked to the temp location when ran.

Refer here.

Sathish Guru V
  • 1,417
  • 2
  • 15
  • 39
  • [New refer link](https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design_3_0.md) since the one linked in the answer is not working anymore – Raphael Apr 07 '21 at 19:39