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

2

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

Fritzfritze answered 17/2, 2015 at 20:25 Comment(6)
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.Delsiedelsman
Silly guess, but what happens when you take your binding out for HttpConfiguration to TestHttpConfiguration?Delsiedelsman
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.Fritzfritze
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.Delsiedelsman
Glad you sorted it too.Fritzfritze
see #28712463 for a similar question.Abscise
F
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.

Fritzfritze answered 23/2, 2015 at 9:18 Comment(3)
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.Delsiedelsman
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.Anders
Instead of creating a new project from scratch, I emptied my bin directory, deleted my packages folder and restored all my packagesHoneysweet
A
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.

Abscise answered 9/7, 2015 at 12:21 Comment(1)
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.Cacology
F
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.

Fritzfritze answered 23/2, 2015 at 9:18 Comment(3)
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.Delsiedelsman
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.Anders
Instead of creating a new project from scratch, I emptied my bin directory, deleted my packages folder and restored all my packagesHoneysweet

© 2022 - 2024 — McMap. All rights reserved.