.net 未找到库 hostpolicy.dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38085430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:49:19  来源:igfitidea点击:

The library hostpolicy.dll was not found

.net.net-coredotnet-cli

提问by Nate Barbettini

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

我有一个简单的 .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].

My project.json looks like this:

我的 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"
}

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

什么是hostpolicy.dll,为什么会丢失?

采纳答案by Nate Barbettini

This error message is unhelpful. The actualproblem is a missing emitEntryPointproperty:

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

  "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 runcan execute.

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

回答by Soleil - Mathieu Prévot

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.

dotnet core 2.0 及更高版本的更新:文件appname.runtimeconfig.json(用于调试和发布配置)与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.

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

回答by Emanuel Lindstr?m

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.

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

回答by homk

For me it was a stupid mistake: I started a wrong file.

对我来说这是一个愚蠢的错误:我启动了一个错误的文件。

回答by sandesh kota

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.

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

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

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

回答by Jonathan DeMarks

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 libcwhen I should have been building for musl.

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

The failing package was built using:

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

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

Changing the RID:

更改 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-x64to linux-musl-x64. If I had read the .NET Core RID Catalog pagethis could have been avoided.

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

回答by iksess

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

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

回答by s k

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

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

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

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

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

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

回答by Imma

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

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

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

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

回答by Ryan Lundy

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.

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

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

我在项目属性页面上(右键单击项目 > 属性)发现目标框架设置为 .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.

我最近更新了 VS 2019,它也安装了 .NET Core 3.1,所以我在下拉列表中切换到那个,它又工作了。

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

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