[英]How to configure self-contained single-file program?
I am experimenting with .Net Core 3
trimmed single-file publishing creating a simple console application and using this to publish: 我正在尝试使用
.Net Core 3
修剪的单文件发布来创建一个简单的控制台应用程序,并使用它来发布:
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
Configuration is done with the standard appsettings.json
and ConfigurationBuilder
from Microsoft.Extensions.Configuration.Json
. 使用
Microsoft.Extensions.Configuration.Json
的标准appsettings.json
和ConfigurationBuilder
完成ConfigurationBuilder
。
The problem is the config file is packaged with the whole app and so is unavailable - it can be accessed in the temp dir created when running the program but that is not very useful for a regular user. 问题是配置文件与整个应用程序打包在一起,因此不可用-可以在运行程序时在创建的temp目录中访问它,但这对普通用户而言不是很有用。
I found out that you can exclude files from the packaging process by editing the csproj file or with a custom publishing profile 我发现您可以通过编辑csproj文件或使用自定义发布配置文件将文件从打包过程中排除
<ItemGroup>
<None Update="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>
This does output the appsettings file in the publish dir however it is not used when running the application - an empty config file is created and used instead. 这确实在发布目录中输出appsettings文件,但是在运行应用程序时不使用它-创建并使用一个空的配置文件。 Is there any way to do this "properly" since documentation is kinda lacking on the issue or should I just manually parse the file I expect to be in the starting directory (which itself was a hassle to find because
Assembly.Get###Assembly
were all returning the temp dir)? 有什么办法可以“适当地”执行此操作,因为在此问题上尚缺少文档,或者我应该手动解析我希望位于起始目录中的文件(由于
Assembly.Get###Assembly
,查找目录本身很麻烦)都返回了临时目录)?
I've found that adding this to the ConfigurationBuilder() before the AddJsonFile() seems to do the trick: 我发现在AddJsonFile()之前将其添加到ConfigurationBuilder()似乎可以解决问题:
.SetBasePath(Environment.CurrentDirectory)
ie: 即:
var configurationRoot = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appSettings.json", optional: false)
.Build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.