9

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

vm2013
  • 304
  • 2
  • 14
  • I just started facing this issue on a new project as well. The middleware throwing this error is: app.UseNinjectWebApi(config); When I comment that out everything works as expected. I am still digging around to figure out what is going on. – sgmeyer Feb 20 '15 at 11:28
  • 1
    Silly guess, but what happens when you take your binding out for HttpConfiguration to TestHttpConfiguration? – sgmeyer Feb 20 '15 at 11:29
  • Hi thanks for replying to me, sadly I didn't manage to get to work my original project. See my answer below I sorted it by starting from scratch and installing the packages using the console. Like you it was the app.useNInjectWebApi that caused the error but no matter how I handled it, it kept failing on this httpconfiguration. Not sure that will help you, I hope it does. Let me know. – vm2013 Feb 23 '15 at 09:19
  • Yeah, I ended up doing the exact same thing. I am not sure what was going on, but it worked for me as well starting from scratch. – sgmeyer Feb 23 '15 at 10:48
  • Glad you sorted it too. – vm2013 Feb 23 '15 at 16:09
  • see http://stackoverflow.com/questions/28711963/ninject-activationexception-thrown-only-on-first-web-request-webapi-2-own-3-n for a similar question. – ngm Jul 09 '15 at 12:20

2 Answers2

9

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.

ngm
  • 7,277
  • 1
  • 53
  • 62
  • 7
    You may have to manually delete the contents of the `/bin` folder. Mine had a DLL file for `Ninject.Web.WebApi.WebHost` in it despite the fact that I had removed the NuGet and References. Even a Clean and Rebuild didn't delete them. I manually deleted all the contents of `/bin` and everything started working again. – crush Jan 09 '16 at 02:15
5

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.

vm2013
  • 304
  • 2
  • 14
  • 13
    I found the issue to be this nuget package "Ninject.Web.WebApi" When using version 3.2.0.0 the error exists. once I updated that nuget package to version 3.2.4.0 everything worked without any additional changes. – sgmeyer Mar 09 '15 at 16:03
  • Everything was working fine in local development for me, but didn't work once deployed to Azure. As per sgmeyer's solution, updating the nuget package to version 3.2.4 solved the problem. – mkaj Dec 20 '15 at 23:29
  • Instead of creating a new project from scratch, I emptied my bin directory, deleted my packages folder and restored all my packages – Ody Jun 22 '16 at 09:12