Haiya! I'm having an issue regarding trying to compile my .NET 5 application to a single file executable while debugging through Visual Studio.
My CSProject is bellow.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net50</TargetFramework>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
I have my runtime identifier set to winx64 and publish single file set to true, yet when building I'm left with a bunch of DLLs that my application uses building along-side it (a total of 272 in total). I was wondering …
roslyn .net-core .net-core-publishsinglefile asp.net-core-5.0
我需要在 Windows PC 上执行 .Net WPF 程序的单个 exe 文件。该文件必须使用代码身份验证证书进行签名。有了这个证书,exe 文件就可以在外部 Windows 系统上执行,而无需系统警告或用户将部署复制到受信任位置后的管理权限。
我可以使用 VS2019 和 .Net 4.7.2 来完成此操作。但我想继续使用 VS2022 和 .Net 6.0。下面是一个使用 VS2019 的简单示例。您可以跳过signtool,因为在VS 2022中已经无法创建单个exe文件。VS 2019 和 .Net 5.0 过去也发生过同样的情况,但我找到了一些帖子,其中包含 WPF 的单个 exe 文件将来首先可以与 .Net 6.0 一起使用的信息。现在,在 .Net 6.0 和 VS2022 发布后,我再次尝试。对于单个文件的部署辅助有更多选项可用,但没有任何东西会创建单个 exe 文件!
VS2019中的工作流程:
我用.Net 6在VS2022中测试流程
deployment wpf .net-core-publishsinglefile .net-6.0 visual-studio-2022
为了更好地解释,我将提供一些背景信息。
我有一个在运行时动态加载库的应用程序。该库是某种插件,应用程序旨在测试该插件。
为了能够调试插件,我使用插件项目打开 VS Code,并将调试器附加到已运行且已加载插件的应用程序。插件程序集已构建并具有一个不错的 PDB 文件。
所以工作流程如下:Application => load plugin => attach debugger
首先,即使应用程序已发布,这也是有效的。这是因为加载的插件是在调试中内置的,因此 PDB 就在那里。
但是,在某些情况下,我似乎无法正确附加或未加载符号。这是如果我发布带有该/p:PublishSingleFile=true标志的应用程序的话。在这种情况下,会生成一个文件,当我尝试附加调试器时,它不起作用。
谁能对此提供任何见解?或者一个可能的解决方案?
值得一提的是,我正在使用 .NET 5
干杯
我有一个目录,其中包含一个独立的“exe”和一个配置文件。exe 必须读取此配置文件。但问题是exe无法获取这个配置文件的正确路径。
我先说一下我是怎么做到的。
using System;
using System.IO;
using System.Reflection;
namespace TestFile {
class Program {
static void Main(string[] args) {
string assemblyLocation = Assembly.GetEntryAssembly().Location;
Console.WriteLine($"assemblyLocation=\"{assemblyLocation}\"");
string configFile = Path.Combine((new FileInfo(assemblyLocation)).DirectoryName, "test.conf");
Console.WriteLine($"configFile=\"{configFile}\"");
File.ReadAllText(configFile);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到这个配置文件的正确路径?
.NET Core 和 .NET 5 具有单文件可执行功能。
如何排除托管程序集被打包到该单个文件中?
示例:我可能希望app.exe包含MyLib.A.dll和MyLib.B.dll,但不包含MyLib.Special.dll。相反,我可能希望它MyLib.Special.dll驻留在旁边的磁盘上app.exe并从那里加载。
(背景:可能是MyLib.Special.dll根据 L-GPL 获得许可的,但是app.exe是专有的。)
如果使用单文件配置发布,应该将数据插入 MySql 数据库的简单控制台应用程序将失败:
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -o publish/ GarLoader.MySqlUploader
Run Code Online (Sandbox Code Playgroud)
如果我随后运行它 ( ./publish/GarLoader.MySqlUploader),它会失败。堆栈跟踪是:
System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.
---> System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception.
---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
---> System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
at System.Reflection.RuntimeAssembly.get_CodeBase()
at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
at System.Configuration.ClientConfigurationSystem..ctor()
at …Run Code Online (Sandbox Code Playgroud) .net c# typeinitializationexception .net-5 .net-core-publishsinglefile
.net-core-publishsinglefile ×6
.net-core ×4
.net-5 ×3
c# ×3
.net ×1
.net-6.0 ×1
deployment ×1
exe ×1
roslyn ×1
typeinitializationexception ×1
wpf ×1