The library hostpolicy.dll was not found
Asked Answered
K

25

121

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?

Kangaroo answered 28/6, 2016 at 20:12 Comment(3)
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. github.com/dotnet/cli/issues/5593#issuecomment-277638612Salena
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 trueEllenaellender
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.Uralaltaic
K
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.

Kangaroo answered 28/6, 2016 at 20:12 Comment(12)
Since this is still happening in rtm it might be worth noting in the github repo just in case.Kept
@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.Facia
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.Pinafore
@UmarFarooqKhawaja I'd recommend posting a new question.Kangaroo
I've seen this happen with csproj files too, targeting netcoreapp1.1 in a aspnetcore project.Jennajenne
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.....Epiboly
@Epiboly Do you know of a fix so you don't have to run it twice? I have the same issue.Soll
@Soll - I do not have a fix, but I have logged it as a bug here: 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.Epiboly
@Epiboly Awesome! Thanks!Soll
@Soll - Updating to 1.1.1 with the security fix and updating to the latest 2017RC solved this problem for me.Epiboly
@Epiboly I'm going to try it out. Thanks for the updates. You're a real champ!Soll
Thanks @Epiboly updating from version 1.1 to 1.1.1 fixed my issueEpiphenomenon
L
71

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.

Laboy answered 11/9, 2017 at 15:28 Comment(3)
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.Stuff
This was the solution for me with dotnet core 3.0 preview 8 alsoTamarind
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.Springclean
K
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.

Kangaroo answered 28/6, 2016 at 20:12 Comment(12)
Since this is still happening in rtm it might be worth noting in the github repo just in case.Kept
@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.Facia
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.Pinafore
@UmarFarooqKhawaja I'd recommend posting a new question.Kangaroo
I've seen this happen with csproj files too, targeting netcoreapp1.1 in a aspnetcore project.Jennajenne
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.....Epiboly
@Epiboly Do you know of a fix so you don't have to run it twice? I have the same issue.Soll
@Soll - I do not have a fix, but I have logged it as a bug here: 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.Epiboly
@Epiboly Awesome! Thanks!Soll
@Soll - Updating to 1.1.1 with the security fix and updating to the latest 2017RC solved this problem for me.Epiboly
@Epiboly I'm going to try it out. Thanks for the updates. You're a real champ!Soll
Thanks @Epiboly updating from version 1.1 to 1.1.1 fixed my issueEpiphenomenon
A
20

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.

Aeromancy answered 23/9, 2022 at 13:20 Comment(2)
This worked in my caseWiltshire
If I had asked this question, @Jeroen Wiert Pllumers' answer would have been the accepted answer. (I.e., it just works.)Spleenwort
G
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.

Gracie answered 8/7, 2020 at 8:6 Comment(0)
S
8

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.

Swollen answered 7/12, 2021 at 17:11 Comment(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.Ringo
E
4

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.

Ephor answered 5/10, 2021 at 19:10 Comment(1)
I used this for my MSTest projects in a solution build where my projects are named 'xxxxUnitTests': $(System.DefaultWorkingDirectory)*UnitTests**\bin***UnitTests.dllDratted
B
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.

Burrell answered 6/11, 2019 at 13:13 Comment(0)
W
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.

Watkin answered 24/2, 2020 at 8:8 Comment(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 ReviewPerilymph
Thank you. I'm just sharing how I fixed the same error in my build. :)Algar
G
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)

Gustin answered 15/5, 2020 at 3:59 Comment(0)
R
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.

Ragland answered 25/9, 2018 at 9:49 Comment(0)
L
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. 😅

Lore answered 9/5, 2019 at 15:53 Comment(0)
P
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.

Pretzel answered 13/9, 2023 at 7:48 Comment(1)
This resolved my issue. Here is the link for the hosting bundle I had to use for 3.1: dotnet.microsoft.com/en-us/download/dotnet/thank-you/…Lawn
A
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...

Anaphylaxis answered 2/3, 2020 at 11:41 Comment(0)
C
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.

Carbamate answered 10/2, 2021 at 10:46 Comment(0)
A
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

Aarika answered 13/9, 2021 at 11:46 Comment(0)
P
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

Phthisic answered 21/12, 2022 at 14:42 Comment(0)
S
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.

Selfforgetful answered 14/2, 2018 at 16:10 Comment(2)
So what did you do to this file, pleaseVizzone
@YoussefSherif You need to copy it from your build directory to Azure. I thought it could be omitted but apparently not.Selfforgetful
E
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.)

Ethic answered 17/10, 2019 at 10:18 Comment(0)
L
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. :)

Linette answered 18/10, 2019 at 21:57 Comment(0)
G
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.

Ganesha answered 4/8, 2022 at 17:56 Comment(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.Unburden
C
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.

Christiansand answered 26/10, 2022 at 10:24 Comment(0)
C
0

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

Candlewick answered 23/5, 2023 at 17:53 Comment(0)
W
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.

Willful answered 12/6, 2023 at 2:14 Comment(0)
E
0

Adding an answer which helped me. Are you perhaps running from the obj directory by mistake? You should use the bin directory.

Eb answered 21/11, 2023 at 11:53 Comment(0)
E
0

The problem can be related when DevOps tries to run tests over Microsoft or other third party DLL instead of just the unit tests of your project.

The default "file matching patterns" lists those dependencies and try to run tests over it, which will fail. The first file for me was the real Test.DLL I need, and the tests passed as expected, the the other binaries fail.

If this is happening, you will see the files on the DevOps pipeline, and also a large count of files will be display, like this:

vstest.console.exe "D:\a\1\s\YourProject.Test\bin\Release\net8.0\YourProject.Test.dll"
"D:\a\1\s\YourProject.Test\bin\Release\net8.0\Microsoft.TestPlatform.AdapterUtilities.dll"
"D:\a\1\s\YourProject.Test\bin\Release\net8.0\Microsoft.TestPlatform.CommunicationUtilities.dll"
"D:\a\1\s\YourProject.Test\bin\Release\net8.0\Microsoft.TestPlatform.CoreUtilities.dll"   "D:\a\1\s\YourProject.Test\bin\Release\net8.0\Microsoft.Testing.Extensions.Telemetry.dll"

(...)
A total of 58 test files matched the specified pattern.

file-matching-patterns: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/file-matching-patterns?view=azure-devops

So you can try to modify your filter to exclude (!) Microsoft or other tests from the pipeline task, for example, this pattern below works for me:

Pattern suggestion:

**\release\**\*test*.dll
!**\testhost.dll
!**\*Microsoft.VisualStudio.Test*
!**\**\*Microsoft.VisualStudio.Test*
!**\*Microsoft.Test*
!**\**\*Microsoft.Test*
!**\obj\**
Estabrook answered 1/2 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.