带有 IIS 的 Kestrel - 运行 上缺少 libuv.dll

Kestrel with IIS - libuv.dll missing on run

我们正在设置一个现有的 Web API 服务器来与现有的 API 一起为站点提供服务。我一直在松散地关注 this article.

这是我的 Global.asax.cs 的样子:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AutoMapperConfig.RegisterMappings();

        var host = new WebHostBuilder()
           .UseKestrel()
           .UseWebRoot("wwwroot")
           .UseIISIntegration()
           .UseStartup<Startup>()
           .Build();

        host.Run();
    }
}

和Startup.cs:

public partial class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseDefaultFiles();
        app.UseStaticFiles();
    }
}

当我运行项目时,我得到错误

Unable to load DLL 'libuv': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

libuv 是 Kestrel 的依赖项。如果我手动将它从 packages 文件夹复制到 bin 文件夹,它就可以工作。这个 GitHub Issue comment 似乎很有意义。现在 project.json 被移走了,我怎样才能让它自动复制?

有些人认为它不知道使用 32 位还是 64 位版本的 libuv,因为在项目属性中将 Platform 设置为 Any CPU。我已经尝试在解决方案和项目设置中将其设置为 x64,但问题仍然存在。

如何让libuv.dll自动直接复制到构建目录?

我不认为将文件包含在项目中(而不是在 packages 文件夹中)并将其设置为复制到输出目录是一个真正的解决方案,只是一个解决方法。我希望找到解决方案,而不是解决方法。

试试这个 - 它可以解决您的问题:

var host = new WebHostBuilder()
            .UseKestrel()
            // .UseWebRoot("wwwroot") keep it if you need it
            .UseContentRoot(Directory.GetCurrentDirectory()) // this could solve your problem
            // .UseUrls("http://0.0.0.0:5000") use this if you're using nginx
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

我以前在迁移项目的时候遇到过类似的问题。 Visual Studio 不匹配的项目可能会出现很多问题。

简单回答:您需要将项目更改为 MSBuild/csproj 格式。

首先,如果您尝试在您的解决方案中使用 .NET Core,请在 Visual Studio 中右键单击您的项目,如果您没有看到 Edit xxxxxx.csproj 那么您可能会遇到与上述问题类似的问题。

Basically, the .NET Core project templates uses different tooling when compiling the project.

下面的解决方案是解决几乎所有与尝试以 .NET Core 为目标但希望使用来自其他框架的库的项目有关的问题的通用方法。没有一个好的工具(到目前为止)来解决这个问题,所以你将不得不进入手动模式。


让我们开始吧。

解决方法很简单(但有点乏味)。

第 1 步:使用新的 MSBuild/csproj 格式创建一个新项目

创建一个新项目,select“.NET Core”。

在几乎所有情况下,您可能希望避免使用 ASP.NET 核心 Web 应用程序模板,但那是另一个讨论。

第 2 步:定位正确的框架

右键单击该项目 select Edit xxxxxx.csproj

<PropertyGroup>
    <TargetFramework>net452</TargetFramework>
    <!--you will also probably want to note that you need these for a console app -->
    <OutputType>Exe</OutputType>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
</PropertyGroup>

选择您想要定位的框架,并确保它受支持(这里是 table)。
我在上面的代码片段中使用了 net452 作为示例。您可以了解有关 naming here.

的更多信息

步骤 3。对所有项目重复。

您将必须为解决方案中的每个项目执行此操作,以防止 Visual Studio 出现意外行为。

网上关于如何让 ASP.NET Core 与旧框架良好协作的内容确实不多。希望这会帮助你。我希望我自己早点得到这个建议。

写下这些代码:

using Microsoft.Owin;
using Owin;
using System.Web.Http;

[assembly: OwinStartup(typeof(WebApiAndStaticFiles.OwinStartup))]

namespace WebApiAndStaticFiles
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();

            HttpConfiguration webApiConfiguration = new HttpConfiguration();
            GlobalConfiguration.Configure(webApiConfiguration); // Instead of GlobalConfiguration.Configure(WebApiConfig.Register);
            app.UseWebApi(webApiConfiguration);

        }
    }
}

并安装这些 nuget 包:

<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.Owin" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.FileSystems" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.StaticFiles" version="3.1.0" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net462" />
  <package id="Owin" version="1.0" targetFramework="net462" />

您不能简单地在 asp.net/iis 托管应用程序管道中使用 asp.net 核心管道。但是 Owin 集成了该管道,这很有魅力。

我只是将 nuget 包安装到我的项目并重新部署。

Install-Package Libuv -版本 1.10.0