Ninject.ActivationException 仅在第一次网络请求时抛出(WebAPI 2、OWIN 3、Ninject 3)
Ninject.ActivationException thrown only on first web request (WebAPI 2, OWIN 3, Ninject 3)
我正在尝试创建一个 "barebones" Web API 项目,该项目使用 OWIN 中间件、Ninject 依赖注入,并最终托管在 IIS 中。我按照文章中的说明进行操作,"Befriending ASP.NET Web.API2, OWIN and Ninject," 此处:http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html,只是我使用了一个空的 Web 应用程序项目,并且在创建项目时没有勾选 "Include Web API references and folders"。
当我启动新的 API 并发出页面请求时,我得到以下 Ninject.ActivationException
抛出:
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 上下文中的 WebAPI 和 Ninject 有点缺乏经验,很难知道问题出在哪里。是 Ninject...吗?它在欧文...?是否在网络中API...?
这是我的 Startup.cs 文件的内容:
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文件的内容:
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的内容:
<?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 的内容同样不引人注目:
<?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>
任何人都可以指出我做错了什么,或者更重要的是,我可以改变什么来让事情正常进行而不抛出异常?
您使用的 Ninject.Web.WebApi 库是什么版本?看起来 2 周前进行了更改以移动一些东西。在此更改之前(v3.2.3 及更早版本),HttpConfiguration
绑定存在于 Ninject.Web.WebApi.OwinHost.OwinWebApiModule
和 Ninject.Web.WebApi.WebApiModule
的 2 个不同模块中。更改后(新版本为 3.2.4),此绑定仅发生一次。
我遇到了同样的错误,但原因不同:出于某种原因我同时安装了 Ninject.Web.WebApi.WebHost
和 Ninject.Web.WebApi.OwinHost
。
如果您在源代码中查找 OwinWebApiModule.cs and WebApiWebHostModule.cs,两个 Ninject 模块都具有 HttpConfiguration
的绑定。
我删除了 Ninject.Web.WebApi.WebHost
,一切正常。
转到参考资料并搜索 Ninject.web.WebApi.WebHost,然后删除 this.Thats 所有它会开始工作。
我正在尝试创建一个 "barebones" Web API 项目,该项目使用 OWIN 中间件、Ninject 依赖注入,并最终托管在 IIS 中。我按照文章中的说明进行操作,"Befriending ASP.NET Web.API2, OWIN and Ninject," 此处:http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html,只是我使用了一个空的 Web 应用程序项目,并且在创建项目时没有勾选 "Include Web API references and folders"。
当我启动新的 API 并发出页面请求时,我得到以下 Ninject.ActivationException
抛出:
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 上下文中的 WebAPI 和 Ninject 有点缺乏经验,很难知道问题出在哪里。是 Ninject...吗?它在欧文...?是否在网络中API...?
这是我的 Startup.cs 文件的内容:
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文件的内容:
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的内容:
<?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 的内容同样不引人注目:
<?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>
任何人都可以指出我做错了什么,或者更重要的是,我可以改变什么来让事情正常进行而不抛出异常?
您使用的 Ninject.Web.WebApi 库是什么版本?看起来 2 周前进行了更改以移动一些东西。在此更改之前(v3.2.3 及更早版本),HttpConfiguration
绑定存在于 Ninject.Web.WebApi.OwinHost.OwinWebApiModule
和 Ninject.Web.WebApi.WebApiModule
的 2 个不同模块中。更改后(新版本为 3.2.4),此绑定仅发生一次。
我遇到了同样的错误,但原因不同:出于某种原因我同时安装了 Ninject.Web.WebApi.WebHost
和 Ninject.Web.WebApi.OwinHost
。
如果您在源代码中查找 OwinWebApiModule.cs and WebApiWebHostModule.cs,两个 Ninject 模块都具有 HttpConfiguration
的绑定。
我删除了 Ninject.Web.WebApi.WebHost
,一切正常。
转到参考资料并搜索 Ninject.web.WebApi.WebHost,然后删除 this.Thats 所有它会开始工作。