88

我有一个简单的 .NET Core 项目(控制台应用程序),我正在尝试编译和运行。dotnet build成功,但是当我这样做时出现以下错误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].

我的 project.json 看起来像这样:

{
  "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"
}

什么是hostpolicy.dll,为什么不见了?

4

16 回答 16

58

dotnet core 2.0 及更高版本的更新:在与appname.dll相同的路径中需要文件appname.runtimeconfig.json(用于调试和发布配置)。

它包含:

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

然后dotnet.exe exec "path/to/appname.dll" [appargs]工作。

于 2017-09-11T15:28:00.033 回答
26

此错误消息没有帮助。实际问题是缺少属性emitEntryPoint

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

添加后,编译器将通知您任何其他问题(例如缺少static void Main()方法)。成功编译项目将产生dotnet run可以执行的输出。

于 2016-06-28T20:12:24.097 回答
5

如果我没记错的话,您可能遇到问题的一种情况是:您有一个引用另一个应用程序项目(而不是库)的集成项目。在这种情况下,dependentProject.runtimeconfig.json不会被复制到您的集成项目的输出文件夹中,并且您将无法运行dependentProject.exe二进制文件,因为它会抛出The library hostpolicy.dll was not found..

有一个 Github问题解决方法

编辑:应该在 .NET SDK 5.0.200 中修复

于 2020-07-08T08:06:04.030 回答
3

这发生在 Visual Studio 2019 预览版将 .Net Core 升级到最新预览版(特别是 .Net Core 3.1.100-preview2-014569)时。

重新安装/修复 .Net Core 3.0.100 为我解决了这个问题。

于 2019-11-06T13:13:50.270 回答
3

我不知道为什么,但我在执行 \bin 文件夹中的 .exe 文件时遇到了问题,而我的 \obj 文件夹中的 .exe 工作正常。

于 2020-02-24T08:08:27.417 回答
2

对我来说,问题在于版本不匹配。我安装了不同的“.Net core SDK”版本,并在 .json 文件中指定了不同的版本。

一旦我修改了 .json 文件中的版本,应用程序就开始正常工作。

于 2018-09-25T09:49:46.813 回答
2

就我而言,这是因为我为错误的目标发布了一个独立的应用程序。我的意图是在 alpine linux 上运行,但我正在为libc应该为musl.

失败的包是使用以下方法构建的:

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

更改 RID:

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

制作了一个功能包。请注意 RID 从 更改linux-x64linux-musl-x64。如果我阅读了.NET Core RID 目录页面,这本可以避免。

于 2019-05-09T15:53:10.617 回答
2

我在 Dotnet Core 3.1 控制台应用程序中遇到了这个问题。

如果要发布应用程序,请确保将目标运行时设置为已安装在目标机器中的特定运行时。

如果您设置为可移植,它将选择它感觉舒适的任何运行时(您可能没有安装它)

于 2020-05-15T03:59:09.807 回答
1

也许您不想做一个“Console .Net Core”项目,而是一个“Console .Net Framework”项目。它解决了问题,对我来说......

于 2020-03-02T11:41:08.743 回答
1

我的问题是我有2 个 .NET Core App 项目,一个依赖于另一个.NET Core App 项目。 (这样我就可以从另一个应用程序执行该应用程序) 但是 .NET Core 应用程序(具有默认配置) 需要默认情况下不复制的<assembly name>.runtimeconfig.json 文件 (以获取一些启动配置) 。


唯一对我有用的解决方案是添加到项目属性>构建事件(依赖项目)这个命令:

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

但是你也可以手动将<依赖程序集名称>.runtimeconfig.json文件复制到依赖项目中。


请注意,应该有更好更通用的方法来自动为每个 .NET Core 应用程序项目执行此操作。

于 2021-02-10T10:46:14.780 回答
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

于 2021-09-13T11:46:38.773 回答
0

对于在 Azure 上使用 ASP.NET Core 2.0 的我来说,正是 appname.deps.json 做到了这一点。您需要将其从构建目录复制到 Azure。

于 2018-02-14T16:10:02.267 回答
0

我在使用 .NET Core 3.0 WPF 应用程序时遇到了同样的问题,但我发现我的应用程序也无法在 Visual Studio 2019 中运行。

我在项目属性页面(右键单击项目 > 属性)上发现目标框架设置为 .NET Core 3.0。

我最近更新了 VS 2019,它也安装了 .NET Core 3.1,所以我在下拉菜单中切换到它,它再次工作。

(我还必须更新我的快捷方式以指向 netcoreapp3.1 文件夹,而不是之前的 netcoreapp3.0 文件夹。)

于 2019-10-17T10:18:59.463 回答
0

推广 voltrevo 的评论作为答案,因为我认为这应该是问题的最常见情况。构建解决方案时,有时您可能会得到 2 个输出 bin 和 obj 的目录。“Bin”目录包含运行 dotnet.exe 命令所需的一切。只需从 bin 目录运行,一切都会好起来的。:)

于 2019-10-18T21:57:25.790 回答
0

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.

于 2021-10-05T19:10:40.953 回答
0

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 the the .runtimeconfig.json in the test binary folder was above 255 characters. Renaming the module, so the file path becomes shorter, resolved the problem.

于 2021-12-07T17:11:52.003 回答