如何处理 .net core 3.1 自包含单文件发布的 Appsettings [英] How to handle Appsettings for .net core 3.1 self contained single file publish

查看:57
本文介绍了如何处理 .net core 3.1 自包含单文件发布的 Appsettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为 Windows 服务托管的新 .NET Core 3.1 工作程序类.我正在使用模板创建的默认 appsettings.json 和 appsettings.environment.json.在 ConfigureServices 期间从 hostContext 加载 appsettings

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"));
   });

我希望能够在部署后编辑 appsettings,以便我可以在生产中更改设置.它在我的机器上调试期间正常工作.我更新了 csproj 文件以使用以下代码来尝试使 appsettings.json 不包含在单个文件中.

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>

在添加这个之后,发布过程确实创建了单个 exe 以及 3 个 appsettings.json 文件,但没有解决它.

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

当 Windows 服务启动时,它会将单个 exe 扩展到文件夹 C:UsersServiceLogonUserAppDataLocalTemp.netServiceNameSomeRandomThing,其中包含发布时项目中存在的 appsettings.json.不是复制到 exe 旁边的 appsettings.json.如果我删除此文件夹,它会重新创建,但会再次使用发布时存在的 appsettings.json.如何使用单个 exe 发布它如何从同一文件夹中读取 appsettings.json 以便在发布后可以编辑该文件?

When the windows service starts up it expands the single exe to the folder C:UsersServiceLogonUserAppDataLocalTemp.netServiceNameSomeRandomThing 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?

推荐答案

我也遇到了同样的问题,并通过项目文件中的这个简单更改解决了.

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>

这会将 appsettings.json 和其他配置 JSON 文件捆绑到单个文件中,并在运行时解压到临时位置.

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.

参考这里.

这篇关于如何处理 .net core 3.1 自包含单文件发布的 Appsettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆