通过命令行构建 .NET Core 应用程序,以便它可以在没有安装 .NET Core 的机器上运行 [英] Building a .NET Core app via command line, so that it works on a machine without .NET Core installed

查看:33
本文介绍了通过命令行构建 .NET Core 应用程序,以便它可以在没有安装 .NET Core 的机器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是创建一个跨平台(非 Web)控制台应用程序,所以我现在正在探索 .NET Core.

在我之前的 .NET 项目中,我在 Visual Studio 中进行了所有开发,但我还创建了一个批处理/MSBuild 文件,以便我可以构建整个项目(包括设置、NuGet 包、带有二进制文件的 zip 文件等).) 只需单击一下.

当我将它复制到另一台机器上时(安装了 Win 7/.NET 4/.NET Core 未安装),它可以工作.

<小时>

我试图在 Visual Studio 之外获得相同结果

1.dotnet 发布

显然这里缺少 .NET Core 运行时,正如预期的那样,当我尝试在没有安装 .NET Core 的机器(与上面提到的相同)上执行它时,这个应用程序崩溃了.

2.来自发布配置文件的 PowerShell 脚本

我尝试执行 PowerShell 脚本(在我创建发布配置文件时创建)在 Visual Studio 之外,但它失败了,因为脚本需要一些参数,而我不知道要传递什么:

param($publishProperties, $packOutput, $nugetUrl)

脚本中还有这一行:

# 要了解有关此文件的更多信息,请访问 http://go.microsoft.com/fwlink/?LinkId=524327

...但该链接仅指向 .NET Web 开发和工具的登录页面博客.

<小时>

TL;DR

我做错了什么?

我知道 .NET Core 的第一个版本主要关注 ASP.NET,但据我了解,ASP.NET Core 应用程序也只是控制台应用程序,所以我认为现在可以使用基本的控制台应用程序.
另一方面,大多数控制台应用程序入门"文档 仍然缺失,所以也许现在还为时过早,控制台应用程序的 dotnet publish 尚未完成?

几天后我怀疑我没有做错任何事情,这是 .NET Core 命令行工具中的问题,所以我 将其发布到命令行工具的问题跟踪器.

解决方案

问题解决了!
我把它贴在.NET Core 命令行工具的问题跟踪器上,结果发现这是一个dotnet publish 中的错误 - 它没有捆绑 C++ 运行时,这是在没有安装 .NET Core 的机器上执行编译的应用程序所必需的.

  • 临时解决方案是安装 C++ 运行时.

  • 真正的"解决方案三天前在拉取请求中提出,现在包含在最新安装程序中.
    在此版本中,dotnet publish 确实捆绑了 C++ 运行时,因此结果可以在没有 .NET Core 的机器上运行.

My end goal is to create a cross-platform (non-web) console application, so I'm exploring .NET Core right now.

In my previous .NET projects, I did all the development inside Visual Studio, but I also created a batch/MSBuild file so I could build the whole project (including setups, NuGet packages, zip files with binaries etc.) with one single click. Here's an example from a previous project.

In the end, I want to do something similar with my .NET Core test project.
But right now I'm failing at the first step: I'm unable to build it outside Visual Studio, so that the result works on another Windows machine without .NET Core installed.
(in the first step, I'm ignoring the cross-platform part - I'll be happy to get it to work on Windows)


What I have

I managed to get it to work inside Visual Studio 2015 Community Edition as follows:

  1. create new project in Visual Studio: "New Project" ⇒ "Web" ⇒ "Console Application (Package)"

  2. create new publish profile inside Visual Studio ("Build" ⇒ "Publish" in the menu).
    This will create a PowerShell script (and an XML file with settings)

Here's my test project on GitHub.

When I do "Build" ⇒ "Publish" in the menu again, Visual Studio apparently executes the previously created PowerShell script again.
The result is slightly over 90 MB, consists of 825 files in 598 folders, and looks like this:

When I copy it on another machine (Win 7 / .NET 4 installed / .NET Core not installed), it works.


What I tried to get the same result outside Visual Studio

1. dotnet publish

This answer and this answer sound like I can use dnu publish to achieve the same result via the command line.
I understand that parts of .NET Core are still moving targets right now, so apparently dnu is now dotnet instead.

So I tried to execute dotnet publish (and created a batch file) for it:

dotnet publish "%~dp0srcCoreTestVisualStudio" -c Release -r win7-x64 -o "%~dp0
eleasecli"

The result consists of an .exe file and a bunch of DLLs, only 25 files and 1.5 MB, all in one single folder:

Obviously the .NET Core runtime is missing here, and as expected, this app crashes when I try to execute it on a machine without .NET Core installed (the same one as mentioned above).

2. The PowerShell script from the publish profile

I tried to execute the PowerShell script (which was created when I created the publish profile) outside Visual Studio, but it failed because the script expects some parameters and I don't know what to pass:

param($publishProperties, $packOutput, $nugetUrl)

There's also this line in the script:

# to learn more about this file visit http://go.microsoft.com/fwlink/?LinkId=524327

...but the link just points to the landing page of the .NET Web Development and Tools Blog.


TL;DR

What am I doing wrong?

I know that the first release of .NET Core mainly focuses on ASP.NET, but as I understood it, ASP.NET Core apps are just console apps as well, so I thought a basic console app would work now.
On the other hand, most of the console app "getting started" docs are still missing, so maybe it's just too early and dotnet publish for console apps is not finished yet?

Edit after a few days: I'm suspecting that I'm doing nothing wrong and that it's an issue in the.NET Core command line tools, so I posted it to the command line tools' issue tracker.

解决方案

Problem solved!
I posted it on the issue tracker of the .NET Core command line tools, and it turned out that it was a bug in dotnet publish - it didn't bundle the C++ runtime, which is needed to execute the compiled app on a machine without .NET Core installed.

  • The temporary solution was to install the C++ runtime.

  • The "real" solution was made in a pull request three days ago, which is included in the latest installer now.
    With this version, dotnet publish does bundle the C++ runtime, so the result will work on a machine without .NET Core.

这篇关于通过命令行构建 .NET Core 应用程序,以便它可以在没有安装 .NET Core 的机器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆