【发布时间】:2023-03-26 12:05:01
【问题描述】:
我的最终目标是创建一个跨平台(非 Web)控制台应用程序,因此我现在正在探索 .NET Core。
在我之前的 .NET 项目中,我在 Visual Studio 中完成了所有开发,但我还创建了一个批处理/MSBuild 文件,以便我可以构建整个项目(包括设置、NuGet 包、带有二进制文件的 zip 文件等.) 只需单击一下。 Here's an example from a previous project.
最后,我想对我的 .NET Core 测试项目做类似的事情。
但是现在我第一步失败了:我无法在 Visual Studio 之外构建它,因此结果可以在另一台没有安装 .NET Core 的 Windows 机器上运行。
(在第一步中,我忽略了跨平台部分 - 我很乐意让它在 Windows 上运行)
我有什么
我设法让它在 Visual Studio 2015 社区版中工作,如下所示:
在 Visual Studio 中创建新项目:"New Project" ⇒ "Web" ⇒ "Console Application (Package)"
在 Visual Studio 中创建新的发布配置文件(菜单中的“构建”⇒“发布”)。
这将创建a PowerShell script(和an XML file with settings)
Here's my test project on GitHub.
当我再次在菜单中执行“构建”⇒“发布”时,Visual Studio 显然会再次执行之前创建的 PowerShell 脚本。
结果略大于 90 MB,由 598 个文件夹中的 825 个文件组成,如下所示:
当我将它复制到另一台机器上时(安装了 Win 7 / .NET 4 / 安装了 .NET Core 未),它可以工作。
我试图在 Visual Studio 之外得到相同的结果
1。 dotnet 发布
This answer 和 this answer 听起来我可以使用 dnu publish 通过命令行实现相同的结果。
我知道 .NET Core 的某些部分现在仍在移动目标,所以 apparently dnu is now dotnet instead。
所以我尝试为它执行dotnet publish(并创建a batch file):
dotnet publish "%~dp0\src\CoreTestVisualStudio" -c Release -r win7-x64 -o "%~dp0\release\cli"
结果包含一个 .exe 文件和一堆 DLL,只有 25 个文件和 1.5 MB,都在一个文件夹中:
显然这里缺少 .NET Core 运行时,并且正如预期的那样,当我尝试在未安装 .NET Core 的机器上执行该应用程序时(与上面提到的相同),该应用程序崩溃。
2。发布配置文件中的 PowerShell 脚本
我尝试执行the PowerShell script(在我创建发布配置文件时创建)outside Visual Studio,但它失败了,因为脚本需要一些参数并且我不知道要传递什么:
param($publishProperties, $packOutput, $nugetUrl)
脚本中也有这一行:
# to learn more about this file visit http://go.microsoft.com/fwlink/?LinkId=524327
...但链接只指向the landing page of the .NET Web Development and Tools Blog。
TL;DR
我做错了什么?
我知道 .NET Core 的第一个版本主要关注 ASP.NET,但据我了解,ASP.NET Core 应用程序也只是控制台应用程序,所以我认为现在可以使用基本的控制台应用程序。
另一方面,大部分console app "getting started" docs 仍然丢失,所以可能还为时过早,控制台应用程序的dotnet publish 还没有完成?
几天后编辑:我怀疑我没有做错任何事情,这是 .NET Core 命令行工具中的问题,所以我posted it to the command line tools' issue tracker。
【问题讨论】:
-
你见过这个吗? github.com/dotnet/cli/blob/master/Documentation/intro-to-cli.md 你可以尝试运行
dotnet build来生成可运行的资产。 -
@DanielMann:不,我以前没有看到这个。不过,它对我不起作用。首先,
dotnet build在我的机器上不存在。然后我从您的链接安装了最新版本,现在dotnet build存在,但抛出异常:Unhandled Exception: System.TypeLoadException: Could not load type 'Microsoft.DotNet.Tools.Compiler.CompilerCommandApp' from assembly 'dotnet-compile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at Microsoft.DotNet.Tools.Build.Program.Main(String[] args) -
@DanielMann:也许我应该更好file an issue on GitHub?我最初在这里发布问题是因为我认为我做错了什么,但如果
dotnet build是现在的当前方式并且它在我的机器上崩溃,我想这实际上是一个错误。 -
我在尝试在命令行上重现 Visual Studio Publish 时遇到了类似的问题。对我来说,它缺少运行时选项。这是有效运行时值的链接:docs.microsoft.com/en-us/dotnet/core/rid-catalog
标签: c# visual-studio dnx .net-core coreclr