116

I have a simple .NET Core project (console app) that I'm trying to compile and run. dotnet build succeeds, but I get the following error when I do dotnet run:

dotnet run
Project RazorPrecompiler (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in [path].

My project.json looks like this:

{
  "buildOptions": {
    "warningsAsErrors": true
  },
  "dependencies": {
    "Microsoft.AspNetCore.Razor": "1.0.0",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    }
  },
  "description": "Precompiles Razor views.",
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [ ]
    }
  },
  "version": "1.2.0"
}

What is hostpolicy.dll, and why is it missing?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • 2
    I ran into this error when trying to run a custom DotnetCliTool with Visual Studio 2017 RC3 that was missing a runtimeconfig.json. The next VS version will pack it by default. https://github.com/dotnet/cli/issues/5593#issuecomment-277638612 – Cameron Taggart Feb 06 '17 at 15:50
  • The same error may be shown, if you run dotnet MyApp.exe, just run MyApp.exe ["The library 'hostpolicy.dll' required" if run from deploy folder, but emitEntryPoint is true](//stackoverflow.com/a/38333053) – Michael Freidgeim Oct 29 '17 at 12:43
  • 1
    With the release of asp.net core 2.1 the webjob publish task has a bug/regression that can cause this error if you're targeting the full framework. The fix for this is to pin the sdk to 2.1.200 until it's fixed. You can also delete the `run.cmd` files to quickly get your production jobs running again. – Brian Surowiec Jun 08 '18 at 23:26

22 Answers22

69

Update for dotnet core 2.0 and beyond: the file appname.runtimeconfig.json (for both debug and release configuration) is needed in the same path as appname.dll.

It contains:

{
  "runtimeOptions": {
    "tfm": "netcoreapp2.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "2.0.0"
    }
  }
}

then dotnet.exe exec "path/to/appname.dll" [appargs] works.

Soleil
  • 6,404
  • 5
  • 41
  • 61
  • 4
    This answer was partly relevant to me as I'm using dotnet core 2.0 too. Not sure if I did something weird to my workspace, but I also found that I had .dll in both `obj` and `bin` directories. I was in `obj`, and realized that the one in `bin` had this `.runtimeconfig.json` file already. Running that one worked without changes. – voltrevo Dec 02 '17 at 07:33
  • 1
    This was the solution for me with dotnet core 3.0 preview 8 also – scourge192 Aug 14 '19 at 16:38
  • 1
    How is this file `appname.runtimeconfig.json` generated? If I specify output as exe, it's there. But if it's build as library (and run as `dotnet app.dll`, it's not there. – liang Mar 14 '22 at 03:45
26

This error message is unhelpful. The actual problem is a missing emitEntryPoint property:

  "buildOptions": {
    ...
    "emitEntryPoint": true
  },

Once this is added, the compiler will let you know about any other problems (like a missing static void Main() method). Successfully compiling the project will result in an output that dotnet run can execute.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • Since this is still happening in rtm it might be worth noting in the github repo just in case. – Nick Acosta Jun 30 '16 at 05:57
  • 2
    @NickAcosta - I guess these kind of issues are why tooling is preview2 and not rtm (the milestone for the bug is 1.0.0-rtm). Only runtime is rtm. – Pawel Jun 30 '16 at 17:20
  • 29
    How does this work with the new Visual Studio 2017 RC projects, where Microsoft has ditched `project.json` is support of `.csproj` files? As far as I can tell, my project is set up to create an executable. – Umar Farooq Khawaja Dec 24 '16 at 13:44
  • @UmarFarooqKhawaja I'd recommend posting a new question. – Nate Barbettini Dec 27 '16 at 16:42
  • 4
    I've seen this happen with csproj files too, targeting netcoreapp1.1 in a aspnetcore project. – tommed Jan 04 '17 at 13:25
  • 3
    Yes, I have a csproj targeting 1.1 and I have this error on first compile. The app always runs if I compile two times in a row..... – mrfleck Jan 16 '17 at 20:10
  • @mrfleck Do you know of a fix so you don't have to run it twice? I have the same issue. – Carson Jan 25 '17 at 20:46
  • 2
    @Carson - I do not have a fix, but I have logged it as a bug here: https://github.com/dotnet/sdk/issues/672 Some folks are having success with the nightly builds, but I am not. The GitHub issue is on their radar and listed as a known issue. – mrfleck Jan 26 '17 at 23:23
  • @mrfleck Awesome! Thanks! – Carson Jan 26 '17 at 23:33
  • 4
    @Carson - Updating to 1.1.1 with the security fix and updating to the latest 2017RC solved this problem for me. – mrfleck Jan 29 '17 at 19:20
  • @mrfleck I'm going to try it out. Thanks for the updates. You're a real champ! – Carson Jan 29 '17 at 19:21
  • Thanks @mrfleck updating from version 1.1 to 1.1.1 fixed my issue – Martin Jan 25 '18 at 07:30
12

I had this happen with .NET 6.0 where somehow the appname.runtimeconfig.dev.json file was not being generated in the bin/Debug/net6.0/ directory.

The fix was modifying the .csproj file and include this fragment inside the <PropertyGroup> element:

<GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>

I found this solution while searching with https://www.google.com/search?q=net60+runtimeconfig.dev.json at Breaking change: runtimeconfig.dev.json file not generated - .NET | Microsoft Learn with the solution at MSBuild properties for Microsoft.NET.Sdk - .NET | Microsoft Learn:

GenerateRuntimeConfigDevFile

Starting with the .NET 6 SDK, the [Appname].runtimesettings.dev.json file is no longer generated by default at compile time. If you still want this file to be generated, set the GenerateRuntimeConfigDevFile property to true.

<PropertyGroup>
  <GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>
</PropertyGroup>

After applying this to the .csproj file and re-building the project, debugging from Visual Studio Code worked fine including stopping at the breakpoints that I had set previously.

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
8

If I'm not mistaken, one scenario when you can hit the issue is this: You have an integration project that references another application project (not library). In this case, dependentProject.runtimeconfig.json won't be copied to your integration project's output folder and you won't be able to run dependentProject.exe binary because it will throw The library hostpolicy.dll was not found..

There is a Github issue for this and a workaround.

Edit: Should be fixed in .NET SDK 5.0.200.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
7

I had similar problem running tests in VS19.

========== Starting test run ==========

Testhost process exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'. Failed to run as a self-contained app.

After digging into it I found the source of the problem:

The full path in the \<my module\>.runtimeconfig.json in the test binary folder was above 255 characters. Renaming the module, so the file path becomes shorter, resolved the problem.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Sergey L
  • 1,402
  • 1
  • 9
  • 11
  • Exactly our case on the AzureDevops ms hosted pipeline on .NET6. That was pretty untrackable since we haven't been able to look at what's inside ms hosted deployed VM. I have tried to recreate the whole project from the scratch and then get the warning about the path. That was the only hint. Upvote for you @SergeyL. – Kamil Stadryniak May 31 '22 at 11:43
3

This occurred when a Visual Studio 2019 preview upgrade .Net Core to the latest preview (specifically .Net Core 3.1.100-preview2-014569).

Reinstalling/repairing .Net Core 3.0.100 solved the problem for me.

Imma
  • 111
  • 1
  • 3
3

I'm not sure why but I ran in to the problem when executing the .exe file in my \bin folder while the .exe in my \obj folder works fine.

Emanuel Lindström
  • 1,607
  • 16
  • 25
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/25453092) – Sadegh Feb 25 '20 at 21:54
  • 2
    Thank you. I'm just sharing how I fixed the same error in my build. :) – Emanuel Lindström Feb 26 '20 at 07:15
3

I am having this problem in Dotnet Core 3.1 Console application.

If you are publishing your application, make sure that your target runtime set to the specific runtime that you had installed in your target machine.

If you set to portable it will pick whatever runtime that it feels comfortable (which you might not have it installed)

s k
  • 4,342
  • 3
  • 42
  • 61
2

For me the issue was with the version mismatch. I had a different ".Net core SDK" version installed and a different version was specified in .json file.

Once I modified the version in my .json file the application started working fine.

sandesh kota
  • 2,491
  • 1
  • 11
  • 11
2

In my case it was because I was publishing a self-contained application for the wrong target. My intent was to run on alpine linux, but I was building for libc when I should have been building for musl.

The failing package was built using:

dotnet publish --self-contained true --runtime linux-x64 --framework netcoreapp2.1 --output /app

Changing the RID:

dotnet publish --self-contained true --runtime linux-musl-x64 --framework netcoreapp2.1 --output /app

produced a functional package. Notice the RID changed from linux-x64 to linux-musl-x64. If I had read the .NET Core RID Catalog page this could have been avoided.

Jonathan DeMarks
  • 2,373
  • 2
  • 15
  • 14
1

Maybe you didn't want to do a "Console .Net Core" project but a "Console .Net Framework" project. It solves the problem, for me...

iksess
  • 574
  • 4
  • 9
1

My problem was that I have 2 .NET Core App projects and one is dependent on the other. (so I can then execute that application from that other application) But .NET Core applications (with default config) need <assembly name>.runtimeconfig.json file (to get some launch config) which isn't copied by default.


The only solution that worked for me was adding to Project Properties > Build Events (of the dependent project) this command:

COPY "$(SolutionDir)<dependency name>\$(OutDir)<dependency assymbly name>.runtimeconfig.json" "$(SolutionDir)$(ProjectName)\$(OutDir)" /Y

But you can also copy the <dependency assembly name>.runtimeconfig.json file by hand to the dependent project.


Note that there should be a better more generic way to do this for every .NET Core App Project automatically.

WENDYN
  • 650
  • 7
  • 15
1

This error is quite generic. So the real problem can be anything.

In my case (if helps anyone with same issue), I created a Class Library project instead of a Console Application project.

A Class Library DLL can't be runned with MSBuild, even if it has a Main method. Only Console Application DDL can be runned as dotnet <appname>.dll

dchang
  • 1,101
  • 10
  • 13
1

I was getting similar error while running Unit tests using VSTest@2 task in Azure Devops. In my case, the problem was with my testAssemblyVer2 value. I was giving wrong pattern for test dlls.

Below one worked for me.(if you are getting this error with VSTest)

- task: VSTest@2
  displayName: 'Running UnitTests'
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      $(System.DefaultWorkingDirectory)\SrcFolder\BBBB.UnitTests\**\bin\**\*.BBBB.UnitTests.dll
      $(System.DefaultWorkingDirectory)\SrcFolder\AAAAa.UnitTests\**\bin\**\*.AAAA.UnitTests.dll
      !**\*TestAdapter.dll
      !**\obj\**
    platform: x64
    configuration: Debug
    codeCoverageEnabled: true

So try to give correct pattern value for testAssemblyVer2 input. Make sure its filtering only the required dlls.

owesom
  • 81
  • 4
1

Add the OutputType on the PropertyGroup and issue is solved

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

more about this MSBuild can be found here

Nick Stavrou
  • 46
  • 3
  • 10
0

For me with ASP.NET Core 2.0 on Azure, it was the appname.deps.json that did the trick. You need to copy it from your build directory to Azure.

Tim
  • 5,435
  • 7
  • 42
  • 62
Richard
  • 627
  • 7
  • 14
0

I had this same problem with a .NET Core 3.0 WPF app, but I found that my app wouldn't run in Visual Studio 2019 either.

I discovered on the project properties page (right-click on project > Properties) that the Target framework was set to .NET Core 3.0.

I'd recently updated VS 2019 which had also installed .NET Core 3.1, so I switched to that in the dropdown, and it worked again.

(I also had to update my shortcut to point to the netcoreapp3.1 folder instead of the previous netcoreapp3.0 folder.)

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
0

Promoting voltrevo's comment as an answer as I believe this should be the most common case of the problem. When you build your solution, sometimes you might get 2 directories with outputs bin and obj. 'Bin' directory has everything that is needed to run dotnet.exe command. Just run from the bin directory and things should be good. :)

0

For me, the error occurred during the SonarQube coverage scan due to one of the projects had a project reference to a MSTest project.

robel
  • 51
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 08 '22 at 06:09
0

I faced this problem and it took me couple of days to figure out the solution.

  1. Go to Visual Studio Installer.
  2. Click on 'More' option of the Visual Studio.
  3. Select 'Repair'.

It'll take some time for the download and installation. Once it's completed restart the machine and try again. This should solve the issue.

StarLord
  • 707
  • 1
  • 8
  • 21
0

Had the same issue. We installed new frameworks, but never restarted IIS. Restarting IIS via this command fixed it: iisreset /restart

Eternal21
  • 4,190
  • 2
  • 48
  • 63
0

I started receiving this error using .NET Maui .net6 after modifying the launchSettings.json. Apparently the error was being thrown due to invalid configuration option. I renamed the file and allowed it to regenerate and then the error was resolved. When comparing the two I am assuming that one of the values I changed was not compatible with the project.

technified
  • 353
  • 1
  • 2
  • 15