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

标签 rest asp.net-web-api ninject owin

我正在为我的 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.WebHostNinject.Web.WebApi.OwinHost .

如果您在源代码中查找 OwinWebApiModule.csWebApiWebHostModule.cs ,两个 Ninject 模块都有 HttpConfiguration 的绑定(bind)。 .

我删除了我不需要的那个并且一切正常。

关于rest - Web Api 2 + OWIN 3 + NInject.Web.WebApi.OwinHost,仅在启动时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570477/

相关文章:

java - Spring 框架和 RestTemplate : not being able to consume REST Service

php - 我如何在 PHP 中使用 FundAmerica RESTful API?

asp.net-web-api - Paypal :IPN 监听器未收到 IPN 消息

javascript - 如何使用 ASP.NET Web API + AngularJs 基于 cookie 的身份验证来验证 cookie

static - IOC - 具有静态辅助方法的 util 类是否应该与 IOC 连接?

java - 无法让 tomcat 服务器运行 - startup.bat 什么都不做

php - 从 PHP REST Web 服务返回 JSON 结果

unit-testing - WebApi 单元测试 urlhelper 现在为空

asp.net-mvc - Asp.net Mvc : Ninject - IPrincipal

c# - 如何获取 Autofac for WebAPI2 的容器?