sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Running a program using .NET Core

Open manu-st opened this issue 10 years ago • 11 comments

Steps to reproduce

I'm looking how I can use .NET Core on Linux to run a program I compiled myself (i.e. without using the dotnet-cli tools). When I compile CoreCLR myself, I just do corerun myprogram.exe where corerun is in my path and sits along with all the native shared libraries and the .NET assemblies (mscorlib and CoreFX). All is fine but to make the process easier, I'd would rather install .NET Core than having to compile it/redistribute it.

So, I tried dotnet exec myprogram.exe but I get

 Expect required library libhostpolicy.so to be present in [/home/coreclr]
  - This may be because of an invalid .NET Core FX configuration in the directory.

I added myprogram.runtimeconfig.json with the following content:

{
  "runtimeOptions": {
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "1.0.0-rc2-3002702"
    }
  }
}

but it fails with:

Failed to initialize CoreCLR, HRESULT: 0x80070057

So what is the proper way to execute a program using the installed .NET Core.

Environment data

 dotnet --info
.NET Command Line Tools (1.0.0-preview1-002702)

Product Information:
 Version:     1.0.0-preview1-002702
 Commit Sha:  6cde21225e

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  14.04
 OS Platform: Linux
 RID:         ubuntu.14.04-x64

manu-st avatar May 25 '16 06:05 manu-st

@schellap

mellinoe avatar May 25 '16 07:05 mellinoe

Could you please attach a zip file of your application drop?

schellap avatar May 25 '16 09:05 schellap

Is there a reason you can't use the .NET CLI SDK?

If all your app's dependencies are in the app directory, then you need the following:

app.dll -- this is just the managed binary (named as .dll) app.exe -- copy over dotnet.exe as app.exe (note: this step is not future-proof) into the app base hostfxr.dll -- copy over from the .NET Core install base. hostpolicy.dll -- copy over from the .NET Core Shared FX install base.

It might be a good idea to check if you have API Sets in your app base, if it is an old CLR.

hostfxr.dll, hostpolicy.dll and dotnet.exe can also be pulled by pulling Microsoft.NETCore.DotNetHostPolicy nupkg.

If you don't want to do all the above steps manually, try:

md app & cd app
dotnet new
Remove the `"type": "platform"` from project.json
dotnet restore --infer-runtimes
dotnet publish

This should produce app.exe, app.dll, app.deps.json, app.runtimeconfig.json and hostfxr.dll, hostpolicy.dll files into the publish directory.

schellap avatar May 25 '16 09:05 schellap

@manu-silicon since the dotnet tools are still in preview we are not ready to document the output artifacts generated by the SDK. The SDK does do compilation, as you did, but they also generate a layout and supporting artifacts required for running the application with the host. If you want to use a custom process to compile your binary then consider building a working application with the SDK and then replace the generated .dll with your own. This is not likely to work, however, since the supporting artifacts would need to be adjusted for your project as well.

TheRealPiotrP avatar May 26 '16 05:05 TheRealPiotrP

@manu-silicon, do you have app.dll, app.runtimeconfig.json and do a dotnet app.dll, things should work, let me know or attach zipball.

schellap avatar May 26 '16 06:05 schellap

I had a similar problem and I correct it by removing bin and obj folders and project.lock.json. Then i use dotnet restore --infer-runtimes dotnet publish and all work correct.

K1vs avatar May 29 '16 19:05 K1vs

Sorry, never got the notifications sent in the past few days. I'll try what was suggested an report back.

@piotrpMSFT fully understand that what I'm doing is different but using dotnet cli for building large project made of many pre-built assemblies is difficult.

manu-st avatar Jun 01 '16 00:06 manu-st

With various trials and errors and luck, found the reason fro the Failed to initialize CoreCLR, HRESULT: 0x80070057 very early on.

What is happening is that because we cannot specify the path to lookup native shared libraries, I've created symbolic links to them in the directory where the executable is. Those symbolic links are what causes the error. If instead, I copy them, then it starts.

I'm not quite there as some managed code cannot load some assemblies, but I'm definitely making progress.

manu-st avatar Jun 01 '16 00:06 manu-st

Got it working as mentionned before it was due to some mismatch between the referenced assemblies I used (in particular System.Text.RegularExpressions.dll which was version 4.1.0.0 in the reference assemblies, but 4.0.12.xxx as actual version, I simply downgraded the reference assembly to 4.0.10 to make it working).

In any case, I'll still need to figure out the 0x80070057. I'll ask over in CoreCLR.

manu-st avatar Jun 06 '16 23:06 manu-st

@manu-silicon, Could you please pastebin the export COREHOST_TRACE=1?

schellap avatar Jun 07 '16 00:06 schellap

Here is the output: log.txt

manu-st avatar Jun 07 '16 02:06 manu-st