如何处理.net Core 3.1自包含单个文件的Appsettings发布 [英] How to handle Appsettings for .net core 3.1 self contained single file publish
问题描述
我有一个新的.NET Core 3.1 worker类,它作为Windows服务托管.我正在使用由模板创建的默认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"));
});
我希望能够在部署应用程序后对其进行编辑,以便可以在生产环境中更改设置.在我的机器上调试期间,它可以正常工作.我更新了csproj文件,使其具有以下代码,以尝试使appsettings.json不包含在Single文件中.
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:\ Users \ ServiceLogonUser \ AppData \ Local \ Temp.net \ ServiceName \ SomeRandomThing,并且具有发布时项目中存在的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:\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?
推荐答案
我也遇到了同样的问题,并通过对项目文件的简单更改解决了该问题.
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文件捆绑到Single文件中,并在运行时解压缩到临时位置.
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屋!