我正在为我的DI构建一个带有Web API2,Owin 3和NInject Owinhost的rest API.
使用NInject提供的示例,我通过它创建一个“HttpConfiguration”对象并在startup.cs中调用NInject扩展方法,我收到一个错误:
Error activating HttpConfiguration
More than one matching bindings are available.
Matching bindings:
1) binding from HttpConfiguration to method
2) self-binding of HttpConfiguration
3) binding from HttpConfiguration to constant value
Activation path:
1) Request for HttpConfigurationSuggestions:
1) Ensure that you have defined a binding for HttpConfiguration only once.
我的代码如下Startup.cs:
public void Configuration(IAppBuilder app) { Logger.Info("Entering Startup"); config = new HttpConfiguration(); ConfigureOAuth(app); // Web API configuration and services config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter("Bearer")); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new {id = RouteParameter.Optional} ); var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault( t => t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); app.UseNinjectMiddleware(CreateKernel); app.UseNinjectWebApi(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); Logger.Info("Exiting Startup"); } public static StandardKernel CreateKernel() { kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); kernel.Bind<HttpConfiguration>().ToSelf().Named("TestHttpConfiguration"); return kernel; }
奇怪的是当我在浏览器中刷新页面时,错误发生了,这让我相信这只会在应用程序启动时发生.
所以我对此感到困惑.有人遇到过同样的问题吗?
谢谢
文森特
解决方法
我有同样的错误,因为我安装了Ninject.Web.WebApi.WebHost和Ninject.Web.WebApi.OwinHost.
如果您查看源代码为OwinWebApiModule.cs和WebApiWebHostModule.cs,则两个Ninject模块都具有HttpConfiguration的绑定.
我删除了一个我不需要的东西,并且工作正常.