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

15,187

Solution 1

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.

Solution 2

For dnu:

There's an option for dnu publish called --runtime that specifies the runtime to include when publishing. You would use the full runtime name with the command, e.g.:

dnu publish --runtime dnx-clr-win-x86.1.0.0-rc1

For dotnet:

You don't need to specify the runtime or framework versions -- by default, dotnet publish will use the framework from project.json and the current runtime flavor. However, the documentation states that:

dotnet-publish command also requires certain dependencies in the project.json to work. Namely the Microsoft.NETCore.Runtime package must be referenced as a dependency in order for the command to copy the runtime files as well as the application's files to the published location.

Share:
15,187
nedhenry
Author by

nedhenry

Some of my hobby projects: SCM Backup - Makes offline backups of your cloud hosted source code repositories MissileSharp - .NET library to control an USB Missile Launcher SimpleLeague - minimalistic tool to display tabular league data in existing websites. RoboShell Backup - simple personal backup tool (PC → NAS → USB disk) Contact me: mail (at) my website

Updated on June 23, 2022

Comments

  • nedhenry
    nedhenry almost 2 years

    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.