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?

3

23 Answers 23

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.

3
  • 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 2, 2017 at 7:33
  • 1
    This was the solution for me with dotnet core 3.0 preview 8 also
    – scourge192
    Aug 14, 2019 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, 2022 at 3: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.

12
  • 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, 2016 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. Dec 24, 2016 at 13:44
  • 4
    I've seen this happen with csproj files too, targeting netcoreapp1.1 in a aspnetcore project.
    – tommed
    Jan 4, 2017 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, 2017 at 20:10
  • 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, 2017 at 19:20
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.

2
  • 2
    This worked in my case
    – Deepak
    Feb 5 at 16:29
  • 1
    If I had asked this question, @Jeroen Wiert Pllumers' answer would have been the accepted answer. (I.e., it just works.)
    – philologon
    Mar 11 at 1:44
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.

0
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.

1
  • 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. May 31, 2022 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.

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.

2
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Sadegh
    Feb 25, 2020 at 21:54
  • 2
    Thank you. I'm just sharing how I fixed the same error in my build. :) Feb 26, 2020 at 7: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)

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.

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. 😅

2

I had this issue when moving the folder

C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.32 to C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.32

In my case I had an API that runs on ISS and gets hosted there. I found out you can also download a runtime version with a windows hosting bundle. this bundle gets installed in the right folder and has the hostpolicy.dll file included.

This did it for me.

1
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...

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.

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

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.

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

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.)

0
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 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.

2
  • 1
    So what did you do to this file, please Aug 15, 2018 at 3:06
  • 1
    @YoussefSherif You need to copy it from your build directory to Azure. I thought it could be omitted but apparently not.
    – Richard
    Aug 15, 2018 at 7:08
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.

1
  • 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.
    – Community Bot
    Aug 8, 2022 at 6: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.

0

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

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.