28

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.

wonea
  • 3,987
  • 17
  • 71
  • 134
Christian Specht
  • 33,837
  • 14
  • 123
  • 176
  • Have you seen this? https://github.com/dotnet/cli/blob/master/Documentation/intro-to-cli.md You can try running `dotnet build` to generate runnable assets. – Daniel Mann Jan 03 '16 at 19:31
  • @DanielMann: No, I didn't see this before. It doesn't work for me, though. First, `dotnet build` didn't exist on my machine. Then I installed the newest version from your link, and now `dotnet build` exists, but throws an exception: `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)` – Christian Specht Jan 03 '16 at 20:39
  • @DanielMann: Maybe I should better [file an issue on GitHub](https://github.com/dotnet/cli/issues)? I originally posted the question here because I thought I was doing something wrong, but if `dotnet build` is the current way now and it crashes on my machine, I suppose it's actually a bug. – Christian Specht Jan 04 '16 at 11:00
  • I went through a similar issue trying to reproduce the Visual Studio Publish on the command line. For me it was missing the runtime option. Here is a link to the valid run time values: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog – Dale C Jun 21 '18 at 03:29

2 Answers2

20

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.

Christian Specht
  • 33,837
  • 14
  • 123
  • 176
5

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.

Rytmis
  • 29,686
  • 8
  • 57
  • 69
  • 1
    For `dotnet`, [it's `--runtime` or `-r`](https://github.com/dotnet/cli/tree/master/src/Microsoft.DotNet.Tools.Publish#options) as well. But I [**did** use that](https://github.com/christianspecht/CoreTestVisualStudio/blob/master/build-cli.bat) in the code in my question: `-r win7-x64` – Christian Specht Jan 04 '16 at 10:50
  • Where did you pull win7-x64 from? You need to use the full runtime name. See my edited response. – Rytmis Jan 05 '16 at 07:17
  • [It's from here](https://github.com/dotnet/cli/tree/master/src/Microsoft.DotNet.Tools.Publish#options), and according to this link `win7-x64` is the only valid value for Windows. I tried the parameter from your edited response, and I get the following error message: `'...\CoreTestVisualStudio' cannot be published for '' 'dnx-clr-win-x86.1.0.0-rc1'`. Providing the framework (`dnxcore50`, like in project.json) doesn't help either. **Where did *you* pull `dnx-clr-win-x86.1.0.0-rc1` from?** It doesn't look like the version numbers that I see when I run `dnvm list`. – Christian Specht Jan 05 '16 at 08:32
  • Actually, I used dnx-clr-win-x86.1.0.0-beta8 for my test. I pulled it from the list of installed frameworks on my profile, under C:\users\\.dnx\runtimes. However, from the page you linked, it looks like things have changed quite a bit since -beta8, so I may need a moment to get my bearings again... – Rytmis Jan 05 '16 at 09:17
  • Do you have `Microsoft.NETCore.Runtime` listed as a dependency in your project.json? According the page you linked to, it needs to be referenced or the runtime won't be bundled. – Rytmis Jan 05 '16 at 09:35
  • Using one of the runtimes under my profile, I still get the same error message like in my last comment. Good point about `Microsoft.NETCore.Runtime` as a dependency (I over-read that), but it doesn't change anything. The result is larger now (162 files, 25 MB), but still doesn't work on the other machine. BTW, the error message is "MSVCP140.dll missing", which [seems to refer to the C++ runtime](http://stackoverflow.com/q/32998902/6884). So maybe publishing via Visual Studio seems to bundle the C++ runtime as well (although I can't find `MSVCP140.dll` in the output)? – Christian Specht Jan 05 '16 at 12:03