C# – 应该是OWIN自主应用程序使用Ninject OWINHost需要system.web?

前端之家收集整理的这篇文章主要介绍了C# – 应该是OWIN自主应用程序使用Ninject OWINHost需要system.web?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用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,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.

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

问题是当我执行Install-Package Microsoft.AspNet.WebApi来安装ASP.NET Web API时,它还添加了对System.Web.Http.WebHost.dll的引用,结果package.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的引用,并从packages.json中删除Microsoft.AspNet.WebApi和Microsoft.AspNet.WebApi.WebHost,所以当有些人会做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>

这是我的工作类库中的package.json ASP.NET Web API项目:

<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" />
原文链接:https://www.f2er.com/csharp/96800.html

猜你在找的C#相关文章