Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 "%~dp0\src\CoreTestVisualStudio" -c Release -r win7-x64 -o "%~dp0\release\cli" 

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.

like image 442
Christian Specht Avatar asked Jan 03 '16 19:01

Christian Specht


People also ask

How do I run a dotnet console app from the command line?

In . NET Core, it runs from the dll, so you have to just run the application by running the command prompt and using the command - dotnet run. Open your command prompt and go to that folder where your application persists. This resulted in printing "Hello World!" as it is written in our console application.

Which command is used to run .NET Core applications?

All the commands start with driver named dotnet. The driver starts the execution of the specified command. After dotnet, we can supply command (also known as verb) to perform a specific action. Each command can be followed by arguments and options.

Which command is used to publish .NET application as a self-contained app to create a Windows 64 bit executable?

Use the dotnet command to start the appdll> command to start your app. . NET Core 2.1 SDK doesn't produce platform-specific executables for apps published framework-dependent.


1 Answers

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.

like image 126
Christian Specht Avatar answered Oct 09 '22 06:10

Christian Specht