rest – Web Api 2 OWIN 3 NInject.Web.WebApi.OwinHost,仅在启动时出错

前端之家收集整理的这篇文章主要介绍了rest – Web Api 2 OWIN 3 NInject.Web.WebApi.OwinHost,仅在启动时出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为我的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 HttpConfiguration

Suggestions:
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.csWebApiWebHostModule.cs,则两个Ninject模块都具有HttpConfiguration的绑定.

删除了一个我不需要的东西,并且工作正常.

猜你在找的HTML相关文章