Web API & Ninject

1,237 views
Skip to first unread message

Jimmy Sáenz

unread,
Nov 16, 2013, 10:33:27 AM11/16/13
to nin...@googlegroups.com
Hi! What is the recommended extension for integrate the last version of ASP.NET Web API (2) with Ninject? I have Ninject.MVC3 and Ninject.Web.Common but i saw in StackOverflow that it's not appropriate. I find in Nuget this package https://www.nuget.org/packages/Ninject.Web.WebApi/3.0.0.2 but when i try to install it i receive a error: Already referencing a newer version of 'Microsoft.AspNet.WebApi'. Somebody can help me? thanks!

Remo Gloor

unread,
Nov 25, 2013, 2:18:30 AM11/25/13
to nin...@googlegroups.com

Use the latest unstable Ninject.Web.WebAPI

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.
To post to this group, send email to nin...@googlegroups.com.
Visit this group at http://groups.google.com/group/ninject.
For more options, visit https://groups.google.com/groups/opt_out.

st...@ttstrain.com

unread,
Nov 26, 2013, 2:19:08 PM11/26/13
to nin...@googlegroups.com


On Monday, November 25, 2013 1:18:30 AM UTC-6, Remo Gloor wrote:

Use the latest unstable Ninject.Web.WebAPI


I'm facing the same problem - using Ninject in a MVC5 project that also carries WebAPI controllers. At this point it's an 'either/or' proposition; using 3.0.2-unstable-9018 I can get the WebAPI controller to light up but that causes my MVC5 controllers to toss: 'No parameterless constructor defined for this object.' I'm also installing the Log4Net extension - I suppose I should try the install without it - any sense that order of Install-Package has any effect? Should I consider a totally separate project for the WebAPI?

And to verify...the contents of NinjectWebCommon don't change with or without the WebAPI extension, right?

many thnks

just in case any of my install routine can shed any light - follows is what happens when I tear all Ninject out and re-apply:


PM> Uninstall-Package Ninject.Web.WebApi
Removing 'Ninject.Web.WebApi 3.0.2-unstable-9018' from CUWebinars.Web.
Successfully removed 'Ninject.Web.WebApi 3.0.2-unstable-9018' from CUWebinars.Web.
Uninstalling 'Ninject.Web.WebApi 3.0.2-unstable-9018'.
Successfully uninstalled 'Ninject.Web.WebApi 3.0.2-unstable-9018'.

PM> Uninstall-Package Ninject.Web.Common
Removing 'Ninject.Web.Common 3.0.2-unstable-9012' from CUWebinars.Web.
Successfully removed 'Ninject.Web.Common 3.0.2-unstable-9012' from CUWebinars.Web.
Uninstalling 'Ninject.Web.Common 3.0.2-unstable-9012'.
Successfully uninstalled 'Ninject.Web.Common 3.0.2-unstable-9012'.

PM> Uninstall-Package Ninject.Extensions.Logging.Log4net
Removing 'Ninject.Extensions.Logging.Log4net 3.0.1.0' from CUWebinars.Web.
Successfully removed 'Ninject.Extensions.Logging.Log4net 3.0.1.0' from CUWebinars.Web.
Uninstalling 'Ninject.Extensions.Logging.Log4net 3.0.1.0'.
Successfully uninstalled 'Ninject.Extensions.Logging.Log4net 3.0.1.0'.

PM> Uninstall-Package Ninject.Extensions.Logging
Removing 'Ninject.Extensions.Logging 3.0.1.0' from CUWebinars.Web.
Successfully removed 'Ninject.Extensions.Logging 3.0.1.0' from CUWebinars.Web.
Uninstalling 'Ninject.Extensions.Logging 3.0.1.0'.
Successfully uninstalled 'Ninject.Extensions.Logging 3.0.1.0'.

PM> Uninstall-Package Ninject.MVC3
Uninstalling 'Ninject.MVC3 3.0.0.6'.
Successfully uninstalled 'Ninject.MVC3 3.0.0.6'.

PM> Uninstall-Package Ninject
Removing 'Ninject 3.0.2-unstable-9039' from CUWebinars.Web.
Successfully removed 'Ninject 3.0.2-unstable-9039' from CUWebinars.Web.
Uninstalling 'Ninject 3.0.2-unstable-9039'.
Successfully uninstalled 'Ninject 3.0.2-unstable-9039'.

PM> install-Package Ninject
Installing 'Ninject 3.0.1.10'.
You are downloading Ninject from Ninject Project Contributors, the license agreement to which is available at https://github.com/ninject/ninject/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject 3.0.1.10'.
Adding 'Ninject 3.0.1.10' to CUWebinars.Web.
Successfully added 'Ninject 3.0.1.10' to CUWebinars.Web.

PM> install-Package Ninject.MVC3
Attempting to resolve dependency 'Ninject (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'Ninject.Web.Common (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'WebActivator (= 1.5)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (= 1.0.0.0)'.
Installing 'Ninject.Web.Common 3.0.0.7'.
You are downloading Ninject.Web.Common from Ninject Project Contributors, the license agreement to which is available at https://github.com/ninject/ninject.extensions.wcf/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.Web.Common 3.0.0.7'.
Installing 'Ninject.MVC3 3.0.0.6'.
You are downloading Ninject.MVC3 from Remo Gloor,   Ian Davis, the license agreement to which is available at https://github.com/ninject/ninject.web.mvc/raw/master/mvc3/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.MVC3 3.0.0.6'.
Adding 'Ninject.Web.Common 3.0.0.7' to CUWebinars.Web.
File Conflict
File 'App_Start\NinjectWebCommon.cs' already exists in project 'CUWebinars.Web'. Do you want to overwrite it?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "N"):n
'App_Start\NinjectWebCommon.cs' already exists. Skipping...
Successfully added 'Ninject.Web.Common 3.0.0.7' to CUWebinars.Web.
Adding 'Ninject.MVC3 3.0.0.6' to CUWebinars.Web.
Successfully added 'Ninject.MVC3 3.0.0.6' to CUWebinars.Web.

PM> install-Package Ninject.Extensions.Logging
Attempting to resolve dependency 'Ninject (= 3.0.0.0 && < 3.1.0.0)'.
Installing 'Ninject.Extensions.Logging 3.0.1.0'.
You are downloading Ninject.Extensions.Logging from Ninject Project Contributors, the license agreement to which is available at https://github.com/ninject/ninject.extensions.logging/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.Extensions.Logging 3.0.1.0'.
Adding 'Ninject.Extensions.Logging 3.0.1.0' to CUWebinars.Web.
Successfully added 'Ninject.Extensions.Logging 3.0.1.0' to CUWebinars.Web.

PM> install-Package Ninject.Extensions.Logging.Log4net
Attempting to resolve dependency 'Ninject.Extensions.Logging (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'Ninject (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'log4net (= 1.2.11)'.
Installing 'Ninject.Extensions.Logging.Log4net 3.0.1.0'.
You are downloading Ninject.Extensions.Logging.Log4net from Ninject Project Contributors, the license agreement to which is available at https://github.com/ninject/ninject.extensions.logging/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.Extensions.Logging.Log4net 3.0.1.0'.
Adding 'Ninject.Extensions.Logging.Log4net 3.0.1.0' to CUWebinars.Web.
Successfully added 'Ninject.Extensions.Logging.Log4net 3.0.1.0' to CUWebinars.Web.

PM> install-Package Ninject.Web.Common
Attempting to resolve dependency 'Ninject (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'WebActivator (= 1.5)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (= 1.0.0.0)'.
'Ninject.Web.Common 3.0.0.7' already installed.
CUWebinars.Web already has a reference to 'Ninject.Web.Common 3.0.0.7'.

PM> install-Package Ninject.Web.WebApi
Attempting to resolve dependency 'Ninject (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'Ninject.Web.Common (= 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'WebActivator (= 1.5)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (= 1.0.0.0)'.
Attempting to resolve dependency 'AspNetWebApi (= 4.0.20126.16343)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi (= 4.0.20710.0 && < 4.1)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.WebHost (= 4.0.20710.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (= 5.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (= 5.0.0)'.
Attempting to resolve dependency 'Newtonsoft.Json (= 4.5.11)'.
Installing 'Ninject.Web.WebApi 3.0.0.2'.
You are downloading Ninject.Web.WebApi from Remo Gloor, the license agreement to which is available at https://github.com/ninject/ninject.web.webapi/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.Web.WebApi 3.0.0.2'.
Install failed. Rolling back...
install-Package : Already referencing a newer version of 'Microsoft.AspNet.WebApi'.
At line:1 char:1
+ install-Package Ninject.Web.WebApi
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
 
PM> install-Package Ninject.Web.WebApi -pre
Attempting to resolve dependency 'Ninject (= 3.0.0-unstable)'.
Attempting to resolve dependency 'Ninject.Web.Common (= 3.0.0-unstable)'.
Attempting to resolve dependency 'WebActivator (= 1.5)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (= 1.0.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi (= 4.0.20710.0 && < 6.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.WebHost (= 4.0.20710.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (= 5.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (= 5.0.0)'.
Attempting to resolve dependency 'Newtonsoft.Json (= 4.5.11)'.
Installing 'Ninject.Web.WebApi 3.0.2-unstable-9018'.
You are downloading Ninject.Web.WebApi from Remo Gloor, the license agreement to which is available at https://github.com/ninject/ninject.web.webapi/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Ninject.Web.WebApi 3.0.2-unstable-9018'.
Adding 'Ninject.Web.WebApi 3.0.2-unstable-9018' to CUWebinars.Web.
Successfully added 'Ninject.Web.WebApi 3.0.2-unstable-9018' to CUWebinars.Web.

PM> uninstall-Package Ninject.Web.WebApi
Removing 'Ninject.Web.WebApi 3.0.2-unstable-9018' from CUWebinars.Web.
Successfully removed 'Ninject.Web.WebApi 3.0.2-unstable-9018' from CUWebinars.Web.
Uninstalling 'Ninject.Web.WebApi 3.0.2-unstable-9018'.
Successfully uninstalled 'Ninject.Web.WebApi 3.0.2-unstable-9018'.

PM>


Marco Garibaldi

unread,
Nov 26, 2013, 5:29:56 PM11/26/13
to nin...@googlegroups.com
I am using the latest unstable version of Ninject.Web.WebAPI (3.0.2-unstable-9018) in a project with MS Web API 5 but I am getting the following error:

Method 'get_RequestScope' in type 'Ninject.Web.WebApi.NinjectWebApiHttpApplicationPlugin' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.

I am using the standard
NinjectWebCommon.cs class in the App_Start folder. Any idea on what I am doing wrong? Am I supposed to modify the NinjectWebCommon.cs class?

using System;
using System.Web;
using FieryRed.ErcApplication.Core.Interfaces.Service;
using FieryRed.ErcApplication.Service;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;

[assembly: WebActivator.PreApplicationStartMethod(typeof(FieryRed.ErcApplication.WebAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(FieryRed.ErcApplication.WebAPI.App_Start.NinjectWebCommon), "Stop")]

namespace FieryRed.ErcApplication.WebAPI.App_Start
{
    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            Bootstrapper.Initialize(CreateKernel);
        }
       
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            Bootstrapper.ShutDown();
        }
       
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
           
            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IService<Device>>().To<BaseService<Device>>().InRequestScope();
            kernel.Bind<IService<Parcel>>().To<BaseService<Parcel>>().InRequestScope();
        }       
    }
}


st...@ttstrain.com

unread,
Nov 26, 2013, 6:18:51 PM11/26/13
to nin...@googlegroups.com
Same behavior with:
  <package id="Ninject.Web.WebApi-RC" version="3.0.0.22" targetFramework="net45" />

st...@ttstrain.com

unread,
Dec 15, 2013, 6:10:18 PM12/15/13
to nin...@googlegroups.com


On Saturday, November 16, 2013 9:33:27 AM UTC-6, Jimmy Sáenz wrote:
Hi! What is the recommended extension for integrate the last version of ASP.NET Web API (2) with Ninject?

Looking to followup regarding guidance on this. Multiple threads related to Ninject not working with WebAPI2 while it appears  Mr. Gloor expects it to work. Would be nice to see confirmation from others so we can try alternatives. My project is dead in the water without DI and I'd very much like to not transition to Unity.

many and respectful thanks!

Włodzimierz Kesler

unread,
Mar 31, 2014, 4:59:24 AM3/31/14
to nin...@googlegroups.com
1. Uninstall Ninject.Web.WebApi.
2. Uninstall Microsoft.AspNet.WebApi.
3. Install this WebApi version:
Install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.0.1
4. Install this Ninject.Web.WebApi version:
Install-Package Ninject.Web.WebApi -Version 3.2.0
5. Correct web.config - probably System.Web will be in wrong version.

That works for me at this moment:)

Yossu Bonkers

unread,
Apr 6, 2014, 12:16:10 PM4/6/14
to nin...@googlegroups.com


On Monday, 31 March 2014 09:59:24 UTC+1, Włodzimierz Kesler wrote:
1. Uninstall Ninject.Web.WebApi.
2. Uninstall Microsoft.AspNet.WebApi.
3. Install this WebApi version:
Install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.0.1
4. Install this Ninject.Web.WebApi version:
Install-Package Ninject.Web.WebApi -Version 3.2.0
5. Correct web.config - probably System.Web will be in wrong version.


I looked in the installed packages, and filtered by "ninject" but didn't see that first package there at all. 

As it happens, my project doesn't actually use the WebApi at all, so I would be quite happy to remove it altogether, but this didn't seem to help. I open the package manager window, and filtered by "webapi" and removed all packages that were shown. I had to delete the WebApiConfig.cs file as well. However, I still got the same problem.

Any ideas? This has wasted my entire afternoon, and I'm really not happy about it. I just want to get on, I don't want to have to fight with Ninject.

mgphi...@gmail.com

unread,
Apr 29, 2014, 1:34:42 AM4/29/14
to nin...@googlegroups.com
Hello,

There has been a large variance of guidance as to what is needed to utilize Ninject in WebApi2.  If I have created a WebAPI 2 project in Visual Studio 2013.  What  Ninject assemblies are required?  Also where should the bindings be placed?

Thanks in advance,
Mark

Uchitha Ranasinghe

unread,
May 28, 2014, 4:46:05 AM5/28/14
to nin...@googlegroups.com
Hi,

I've had numerous problems with getting it up and running. Here's what I did.

1. Uninstall all Ninject packages that you have associated with your project
2. Make sure your Api works without any dependency injection
3. Now include dependency injection (I used constructor injection) 
4. Test your api now and it should fail - (Not able to find default constructor)
5. Now install the following packages in order - 
  • Ninject.MVC3
  • Ninject.Web.WebApi
These 2 will resolve the other dependencies and also add the file 'NinjectWebCommon.cs' to the App_Start folder
6. Now in the NinjectWebCommon class add your dependency resolvers inside the 'RegisterServices' method. This is the only piece of code I had to hand craft
  • kernel.Bind<IService>().To<ServiceImpl>();
Google did not help as the packages/methods have changed a lot recently. 
Reply all
Reply to author
Forward
0 new messages