Ninject.ActivationException 僅在第一個 Web 請求時引發(WebAPI 2、OWIN 3、Ninject 3)
我正在嘗試創建一個使用 OWIN 中間件、Ninject 依賴注入並最終託管在 IIS 中的“準系統”Web API 項目。我已按照文章“與 ASP.NET Web.API2、OWIN 和 Ninject 交朋友”中的說明進行操作:http ://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html ,除了我使用了一個空的 Web 應用程序項目,並且在創建項目時沒有勾選“包含 Web API 引用和文件夾”。
當我啟動新的 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 上下文和 Ninject 中對 WebAPI 有點缺乏經驗,很難知道問題出在哪裡。是與Ninject…嗎?是在 OWIN 中嗎…?它在WebAPI中……?
這是我的 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綁定存在於 和 中的 2 個不同模組Ninject.Web.WebApi.OwinHost.OwinWebApiModule中Ninject.Web.WebApi.WebApiModule。在此更改之後(新版本為 3.2.4),此綁定僅發生一次。