How to handle Appsettings for .net core 3.1 self contained single file publish

筅森魡賤 提交于 2020-06-15 16:07:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/59366046/how-to-handle-appsettings-for-net-core-3-1-self-contained-single-file-publish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!