Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api 2 + OWIN 3 + NInject.Web.WebApi.OwinHost, error at startup only

I am building a rest API with Web API2, Owin 3 and NInject Owinhost for my DI.

Using the example provided by NInject, by which I create an "HttpConfiguration" object and call the NInject extension methods in startup.cs, I get an error:

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.

My code is as follow in 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;
    }

The strange thing is when I refresh the page in the browser, the error goes, which leads me to believe that this happens at application startup only.

So I'm confused with this. Has anyone faced the same issue with it?

Thanks

Vincent

like image 508
vm2013 Avatar asked Feb 17 '15 20:02

vm2013


2 Answers

I had this same error, as for some reason I had installed both Ninject.Web.WebApi.WebHost and Ninject.Web.WebApi.OwinHost.

If you look in source for OwinWebApiModule.cs and WebApiWebHostModule.cs, both Ninject modules have a binding for HttpConfiguration.

I removed the one I didn't need and things worked.

like image 194
ngm Avatar answered Nov 12 '22 00:11

ngm


UPDATE

After trying everything, I managed to get it to work by... Starting a new project from scratch. I had the luxury of doing this because it is a new proof of concept for me.

The main difference here is I installed the packages required (owin 3, ninject) using the Package Manager console rather than the UI. I followed this link here to install those packages.

I then noticed an error message on one of the package as it was looking for Owin 2 dependencies and could not find any. I forced it to install using -DependencyVersion Highest as parameter and it was working fine from the outset.

Unless I missed it I didn't see this error when I installed the packages using the UI. Is it possible the package didn't install properly previously on my other project? Not sure.

Hope this helps someone.

like image 24
vm2013 Avatar answered Nov 12 '22 00:11

vm2013