gpt4 book ai didi

c# - 使用 Ninject OWINHost 的 OWIN 自托管应用程序是否需要 system.web?

转载 作者:可可西里 更新时间:2023-11-01 08:20:52 28 4
gpt4 key购买 nike

我正在尝试使用带有 Ninject 的 OWIN 自托管 WebAPI 创建 Windows 服务。我让它工作,但我必须添加对 system.web 的引用,这似乎是错误的。在没有引用 system.web 的情况下,我得到了这些编译错误:

The type 'System.Web.Routing.RouteCollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

The type 'System.Web.Routing.Route' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

按照这篇文章添加Ninject后出现的错误 Setting up a OWIN WebApi application

我还必须将 Microsoft.Owin 限制为 2.1.0 版,Ninject 才能工作。在服务启动期间,Ninject 正在寻找 Owin 2.1.0。如果您获得 Microsoft.Owin 的最新版本,它将升级到 3.0。

我使用的三个主要 NuGet 包是:

Microsoft.AspNet.WebApi.OwinSelfHost

Ninject.Web.Common.OwinHost

Ninject.Web.WebApi.OwinHost

这是我所有的包(注意 Microsoft.Owin 上的限制)

  <package id="Microsoft.AspNet.WebApi" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" allowedVersions="(,2.1]" />
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
<package id="Ninject.Extensions.ContextPreservation" version="3.2.0.0" targetFramework="net45" />
<package id="Ninject.Extensions.NamedScope" version="3.2.0.0" targetFramework="net45" />
<package id="Ninject.Web.Common" version="3.2.2.0" targetFramework="net45" />
<package id="Ninject.Web.Common.OwinHost" version="3.2.2.0" targetFramework="net45" />
<package id="Ninject.Web.WebApi" version="3.2.1.0" targetFramework="net45" />
<package id="Ninject.Web.WebApi.OwinHost" version="3.2.1.0" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />

Windows服务的Program.cs是这样的

using Microsoft.Owin.Hosting;
using Ninject;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;
using System.Reflection;
using System.ServiceProcess;
using System.Web.Http;


namespace ServiceExample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};


using (WebApp.Start<Startup>("http://localhost:12345"))
{
ServiceBase.Run(ServicesToRun);
}
}



public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureWebAPI(app);

}

private void ConfigureWebAPI(IAppBuilder app)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new {id = RouteParameter.Optional});

app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);
}


private static StandardKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
}
}
}

最佳答案

尝试从引用中删除 System.Web.Http.WebHost.dll

references list with System.Web.Http.WebHost.dll


当我想要一个 API 项目作为类库和单独的主机项目(一个使用控制台和其他 IIS)时,我遇到了同样的问题,如 this great blog post 中所述

问题是,当我执行 Install-Package Microsoft.AspNet.WebApi 来安装 ASP.NET Web API 时,它还添加了对 System.Web 的引用.Http.WebHost.dll 和结果 packages.json 是:

<packages>
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>

所以解决方案是删除对 System.Web.Http.WebHost.dll 的引用并删除 Microsoft.AspNet.WebApiMicrosoft.AspNet.WebApi .WebHost 来自 packages.json 因此当某些人执行 nuget 恢复/重新安装时它不会再次安装:

<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>

这是我来自工作类库 ASP.NET Web API 项目的 packages.json:

<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />

关于c# - 使用 Ninject OWINHost 的 OWIN 自托管应用程序是否需要 system.web?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689017/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com