dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.73k stars 1.07k forks source link

dotnet run app.dll throws null reference exception #5995

Closed javiercn closed 4 years ago

javiercn commented 8 years ago

Steps to reproduce

  1. build a console app.
  2. run dotnet publish
  3. navigate to the publish folder
  4. run dotnet run app.dll

    Expected behavior

An error indicating that to use dotnet on a dll you shoud do dotne app.dll

Actual behavior

Object reference not set to an instance of an object.

Environment data

dotnet --info output: .NET Command Line Tools (1.0.0-rc2-002611)

Product Information: Version: 1.0.0-rc2-002611 Commit Sha: bf8f0edd89

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

Other info "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0-rc2-24008", "type": "platform" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-20581", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-20581" },

"frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } }

moozzyk commented 8 years ago

Hit this today as well and reopened https://github.com/dotnet/cli/issues/941

vcsjones commented 8 years ago

This is related to dotnet/cli#2965. CLI is looking for a project.json in the current path and wants to pass "app.dll" as an argument.

eerhardt commented 8 years ago

I think this was fixed with dotnet/sdk#5994. Can you try again with the latest Preview2 build and close if this has been fixed?

BTW - to run your published app, you don't say dotnet run. Instead you call, dotnet myapp.dll. dotnet run is a "development time" command, thus it needs a project.json file, and your project to be restored, etc.

Sebazzz commented 8 years ago

Perhaps this can be clarified in documentation. I was trying to run my published app using dotnet run, only to find out now it should be run using dotnet filename.dll. Never mind, it shouldn't crash with such a vague error message. I haven't tried the preview2 bits yet though.

toddams commented 8 years ago

dotnet run is a "development time" command, thus it needs a project.json file, and your project to be restored, etc.

@eerhardt But when you actually use dotnet run app.dll on published self-contained app - it works. That is an expected behaviour?

304NotModified commented 8 years ago

Perhaps this can be clarified in documentation.

Agree with this. I got the same problem, and fixed it with this thread.

eerhardt commented 8 years ago

But when you actually use dotnet run app.dll on published self-contained app - it works. That is an expected behaviour?

Can you describe exactly the steps you took and which directory you were running from? The command can work if you execute it from the project directory:

mkdir MyTest
cd MyTest
dotnet new

Change project.json:
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": "1.0.0-rc2-3002702"
  },
  "frameworks": {
    "netcoreapp1.0": {}
  },
  "runtimes": {
    "win7-x64": {}
  }
}

dotnet restore
dotnet publish
dotnet run bin\Debug\netcoreapp1.0\win7-x64\publish\MyTest.dll

This only works by accident for 2 reasons:

  1. You are running from the same directory as your project.json, so dotnet run is finding the project file in the current directory.
  2. The bin\Debug\netcoreapp1.0\publish\MyTest.dll is being interpreted as a command line argument into your app, and it is being passed into your app. You can confirm this by printing the arguments to the console.

If however, I say:

cd bin\Debug\netcoreapp1.0\win7-x64\publish
dotnet run MyTest.dll

I get Object reference not set to an instance of an object.

vcsjones commented 8 years ago

@toddams

But when you actually use dotnet run app.dll on published self-contained app - it works. That is an expected behavior?

I suspect it is not running app.dll, but it found a project.json in the directory that you are running it from, and that is what is getting run.

blackdwarf commented 7 years ago

This has been added to the documentation now (e.g. https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-run). Closing. Since the issue is by design, if the docs need more clarification, feel free to open the issue on dotnet/docs repo.