Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c# - Building a .NET Core app via command line, so that it works on a machine without .NET Core installed

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:

Visual Studio publish result

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:

dotnet publish result

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...