Ninject.Activati​​onException只在第一次Web请求抛出(2的WebAPI,拥有3 Ninject 3) [英] Ninject.ActivationException thrown only on first web request (WebAPI 2, OWN 3, Ninject 3)

查看:314
本文介绍了Ninject.Activati​​onException只在第一次Web请求抛出(2的WebAPI,拥有3 Ninject 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用OWIN中间件,Ninject DI,最终在IIS中承载的准系统的WebAPI项目。我跟随的文章中找到的说明,益友ASP.NET Web.API2,OWIN和Ninject,这里:<一href=\"http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html\">http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html,除了我用了一个空的Web应用程序项目,并没有勾选包含的WebAPI引用和文件夹创建项目时。

I am attempting to create a "barebones" WebAPI project that uses OWIN middleware, Ninject DI, and ultimately to be hosted in IIS. I have followed instructions found in an article, "Befriending ASP.NET Web.API2, OWIN and Ninject," here: http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html, except I used an empty Web Application Project, and did not tick the "Include WebAPI references and folders" when creating the project.

当我火了新的API,使一个页面请求,我得到以下Ninject.Activati​​onException抛出:

When I fire up the new API and make a page request, I get the following Ninject.ActivationException thrown:

Server Error in '/' Application.

Error activating HttpConfiguration
More than one matching bindings are available.
Matching bindings:
 1) binding from HttpConfiguration to method
 2) binding from HttpConfiguration to constant value
Activation path:
 1) Request for HttpConfiguration

Suggestions:
 1) Ensure that you have defined a binding for HttpConfiguration only once.    

这个异常消失,当我刷新我的浏览器。我真不明白是什么原因造成这个异常。从我的角度来看,是在OWIN背景和Ninject的WebAPI有些经验不足,很难知道问题所在。它是用Ninject ...?它是在OWIN ...?难道是...的WebAPI?

This exception goes away when I refresh my browser. I really don't understand what is causing this exception. From my point of view, being somewhat inexperienced with WebAPI in OWIN context and Ninject, it's hard to know where the problem lies. Is it with Ninject...? Is it in OWIN...? Is it in WebAPI...?

这是我的Startup.cs文件的内容:

This is the content of my Startup.cs file:

using System.Web.Http;
using LHD.API_2;
using Microsoft.Owin;
using Microsoft.Owin.Security.OAuth;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;

[assembly: OwinStartup(typeof(Startup))]

namespace LHD.API_2
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();

            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute("API Default", "api/1/{controller}/{action}/{id}", new { id = RouteParameter.Optional });

            app.UseNinjectMiddleware(() => NinjectConfig.CreateKernel.Value);
            app.UseNinjectWebApi(config);
        }
    }
}

这是我的NinjectConfig.cs文件的内容:

This is the content of my NinjectConfig.cs file:

using System;
using System.Reflection;
using Ninject;

namespace LHD.API_2
{
    public static class NinjectConfig
    {
        public static Lazy<IKernel> CreateKernel = new Lazy<IKernel>(() =>
        {
            StandardKernel kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            RegisterServices(kernel);

            return kernel;
        });

        private static void RegisterServices(KernelBase kernel)
        {
            // TODO - put in registrations here...

            //kernel.Bind<IFakeService>().To<FakeService>();
        }
    }
}

这是我的packages.config的内容:

This is the content of my packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net451" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net451" />
  <package id="Ninject" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Extensions.ContextPreservation" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Extensions.NamedScope" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.Common.OwinHost" version="3.2.3.0" targetFramework="net451" />
  <package id="Ninject.Web.WebApi" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.WebApi.OwinHost" version="3.2.4.0" targetFramework="net451" />
  <package id="Owin" version="1.0" targetFramework="net451" />
</packages>

...而且,为完整起见,我的web.config的含量同样引人注意:

... and, for the sake of completeness, the content of my web.config is similarly unspectacular:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
    </system.web>

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

任何人都可以点我朝着我做错了什么,或者更重要的是,有什么我可以改变得到的东西没有例外的工作被抛出?

Can anyone point me towards what I'm doing wrong, or more importantly, what I can change to get things working without the exception being thrown?

干杯,
   乔恩

Cheers, Jon

推荐答案

什么版本的 Ninject.Web.WebApi 库您使用的?它看起来像2个星期前发生了改变,移动周围的一些事情。这种变化(V3.2.3和更早版本)之前, HttpConfiguration 装帧在2个不同的模块present在 Ninject.Web.WebApi.OwinHost。 OwinWebApiModule Ninject.Web.WebApi.WebApiModule 。这种变化(新版本3.2.4是)后,此绑定只发生一次。

What version of the Ninject.Web.WebApi library are you using? it looks like 2 weeks ago a change was made to move some things around. before this change (v3.2.3 and earlier), HttpConfiguration bindings were present in 2 different modules in Ninject.Web.WebApi.OwinHost.OwinWebApiModule and Ninject.Web.WebApi.WebApiModule. after this change (new version is 3.2.4), this binding only happens once.

这篇关于Ninject.Activati​​onException只在第一次Web请求抛出(2的WebAPI,拥有3 Ninject 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆