0

I've installed the Ninject.Web.WebAPI package using Nuget and i cant figure out how to bind my dependencies.

I regularly use Ninject with my MVC apps and when i install the Ninject package it creates a NinjectWebCommon.cs file where i set up my bindings. This file doesnt appear to generate for the WebAPI project.

Anyone help me out on where to put my bindings.

cheers

markusrambarkus
  • 412
  • 1
  • 5
  • 12

1 Answers1

1

Create a class to fix NinjectDependencyResolver issue of web api.

public class NinjectDependencyResolverHelper : IDependencyScope
    {
        IResolutionRoot resolver;
        public NinjectDependencyResolverHelper(IResolutionRoot resolver)
        {
            this.resolver = resolver;
        }
        public object GetService(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has been disposed");
            return resolver.TryGet(serviceType);
        }
        public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has been disposed");
            return resolver.GetAll(serviceType);
        }
        public void Dispose()
        {
            var disposable = resolver as IDisposable;
            if (disposable != null)
                disposable.Dispose();
            resolver = null;
        }
    }
    public class NinjectDependencyResolver : NinjectDependencyResolverHelper, IDependencyResolver
    {
        IKernel kernel;
        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            this.kernel = kernel;
        }
        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyResolverHelper(kernel.BeginBlock());
        }
    }

Then create NinjectWebCommon class

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>();

        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        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)
    {
        //Bind your stuff            
    }        
}

Hope this helps.

DSR
  • 4,588
  • 29
  • 28
  • Actually, latest version of `Ninject.Web.WebApi` ("Ninject Integration for WebApi 2", since at least version 3.2.1.0) requires nothing custom. Just add the package and register everything as normal in NinjectWebCommon.cs. – Chris Pratt Sep 22 '14 at 15:06