简体   繁体   English

如何发布引用另一个可执行文件的独立 C# 可执行文件?

[英]How to publish a self-contained C# executable that references another executable?

I have two C# .NET 5 projects, let's call them A and B. Both are executables (not DLLs).我有两个 C# .NET 5 个项目,我们称它们为 A 和 B。两者都是可执行文件(不是 DLL)。 Project A references project B. I would like to publish project A as a self-contained executable (though not single-file, if that matters).项目 A 引用项目 B。我想将项目 A 发布为独立的可执行文件(尽管不是单个文件,如果重要的话)。 I'm using this command to kick off the publishing process:我正在使用此命令启动发布过程:

do.net publish A.csproj -c Release --self-contained -r win-x64 /p:PublishTrimmed=true /p:PublishReadyToRun=true

However, this produces the following error when attempting to build project B (which is required since project A references it):但是,这在尝试构建项目 B 时会产生以下错误(这是必需的,因为项目 A 引用了它):

error.NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. You must either specify a RuntimeIdentifier or set SelfContained to false.

But as you can see, I am (I think?) specifying a runtime identifier with -r win-x64 .但如您所见,(我认为?)使用-r win-x64指定运行时标识符。 I'm guessing that the runtime identifier that I've specified on the command-line isn't getting passed through when building project B. How can I fix this without disabling SelfContained?我猜我在命令行上指定的运行时标识符在构建项目 B 时没有通过。如何在不禁用 SelfContained 的情况下解决这个问题?

My do.net version is:我的 do.net 版本是:

dotnet --version
5.0.400

EDIT: I do not want to put <RuntimeIdentifier>win-x64</RuntimeIdentifier> in my project files because I need to build for multiple different runtimes and I want to be able to control that via the command line.编辑:我不想将<RuntimeIdentifier>win-x64</RuntimeIdentifier>放在我的项目文件中,因为我需要为多个不同的运行时构建,并且我希望能够通过命令行控制它。

Thanks to a couple of comments in this Github issue , I was able to figure out how to get it to compile:感谢Github 问题中的一些评论,我能够弄清楚如何让它编译:

  1. Remove the --self-contained flag from the command-line.从命令行中删除--self-contained标志。 (According to the docs , "Default is true if a runtime identifier is specified and the project is an executable project (not a library project).") (根据文档,“如果指定了运行时标识符并且项目是可执行项目(而不是库项目),则默认值为真。”)

  2. Add /p:ValidateExecutableReferencesMatchSelfContained=false to the command-line.添加/p:ValidateExecutableReferencesMatchSelfContained=false到命令行。 This was required to suppress the error: "The referenced project 'B.csproj' is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable."这是抑制错误所必需的:“引用的项目‘B.csproj’是一个非独立的可执行文件。独立的可执行文件不能引用非独立的可执行文件。”

  3. If you want to be able to run the B.exe file that was generated while building project A, you'll need to add an appropriate B.deps.json file to the build output. The easiest way I've found to do this is to make a separate self-contained build of B and then simply copy over the B.deps.json file from it.如果您希望能够运行在构建项目 A 时生成的B.exe文件,则需要将适当的B.deps.json文件添加到构建 output 中。这是我发现的最简单的方法是制作 B 的独立自包含构建,然后简单地从中复制B.deps.json文件。 (And if you're building with PublishTrimmed as I am, then you may need to add entries into your TrimmerRoots.xml file to prevent code needed by project B from getting trimmed from the output.) (如果您像我一样使用 PublishTrimmed 进行构建,那么您可能需要将条目添加到您的TrimmerRoots.xml文件中,以防止项目 B 所需的代码从 output 中删除。)

After doing these two things, my project is now building and running successfully.完成这两件事后,我的项目现在正在构建并成功运行。 I have verified that it still works even on a machine that does not have the .NET Runtime installed and thus the build really is self-contained as desired.我已经验证即使在没有安装 .NET 运行时的机器上它仍然可以工作,因此构建确实是独立的。

I also have Library, Console and UI projects and having same problem.我也有库、控制台和 UI 项目并且有同样的问题。 I referenced console project into UI to be copied over to the output folder of UI in development and publish time, but the best solution is just to unreference the console project and publish both for same folder我将控制台项目引用到 UI 中,以便在开发和发布时复制到 UI 的 output 文件夹,但最好的解决方案是取消引用控制台项目并将两者发布到同一个文件夹

dotnet publish MyConsole -o "publish/myRelease" -c Release -r xxx-xxx -p:PublishReadyToRun=true --self-contained
dotnet publish MyUI-o "publish/myRelease" -c Release -r xxx-xxx -p:PublishReadyToRun=true --self-contained

My build scripts:我的构建脚本:

  1. https://github.com/sn4k3/UVtools/blob/master/build/createRelease.ps1 https://github.com/sn4k3/UVtools/blob/master/build/createRelease.ps1
  2. https://github.com/sn4k3/UVtools/blob/master/build/createRelease.sh https://github.com/sn4k3/UVtools/blob/master/build/createRelease.sh

I only call the ps1 script on windows and let WSL do the rest for others.Unfortunately compiling to windows on linux generate a "bad".exe我只在 windows 上调用 ps1 脚本,让 WSL 为其他人执行 rest。不幸的是,在 linux 上编译为 windows 会生成一个“坏”.exe

Note: If you want the other project to be copied during development, eg: Visual Studio, you can create a simple copy over command on build conditions.注意:如果您希望在开发过程中复制其他项目,例如:Visual Studio,您可以在构建条件下创建一个简单的复制命令。 That will fix both problems这将解决这两个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将MsTest项目构建为独立的可执行文件? 怎么样? - Is it possible to build an MsTest project into a self-contained executable? How? .NET Core 自包含发布引用 - 如何删除未使用的? - .NET Core self-contained publish references - how to remove unused? 引用的项目是一个非独立的可执行文件。 自包含的可执行文件不能引用非自包含的可执行文件 - The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable 检测是否从已发布的自包含可执行文件运行 - Detect if running from published self-contained executable .NET 6.0 控制台应用程序作为独立的单文件可执行文件 - .NET 6.0 Console app as a self-contained single file executable 例如 C# 自包含 getter - C# Self-contained getter for instance 关闭 Visual Studio 后生成的自包含可执行文件无法运行 - Generated self-contained executable file failed to run after I close Visual Studio C#.NET 4自包含的Oracle驱动程序部署 - C# .NET 4 Self-Contained Oracle Driver Deployment 将Javascript / JQuery存储在独立的C#类中 - Storing Javascript/JQuery in a Self-Contained C# class 如何在内部使用IronPython引用自包含的C#类库项目(Visual Studio 2010) - How to refer self-contained C# class library project with IronPython inside (Visual Studio 2010)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM