Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ninject with Ninject.Web.Api for Web Api 2 is not working in ASP.NET MVC 5

I am developing an Asp.NET MVC project. My project has web api as well. I am using ASP.NET MVC5 and Web Api 2 with Visual Studio 3. I am doing dependency injection using ninject. I know ninject for web is not working for Web Api 2. So I tried to use Ninject for Web Api.

I installed ninject for web api 2 package using nuget package manager

enter image description here

Then I installed Ninject.Web using nuget package manager

enter image description here

Then in NinjectWebCommon, I added this line in RegisterServices

private static void RegisterServices(IKernel kernel)
        {
            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
            kernel.Bind<ICategoryRepo>().To<CategoryRepo>();
        }    

This is my full NinjectWebCommon class registering one dependency

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon), "Stop")]

namespace PatheinFashionStore.Web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using PatheinFashionStore.Domain.Abstract;
    using PatheinFashionStore.Domain.Concrete;

    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();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {

            kernel.Bind<ICategoryRepo>().To<CategoryRepo>();
        }        
    }
}

This is my Controller

 public class HomeController : Controller
    {
        private ICategoryRepo categoryRepo;

        public HomeController(ICategoryRepo categoryRepoParam)
        {
            this.categoryRepo = categoryRepoParam;
        }

        public ActionResult Index()
        {
            return View();
        }
}

Then when I run my code, it is giving me this error

enter image description here

Update

But when I access apiController, it is working.

Here is my web api controller

 public class TestController : ApiController
    {
        private ICategoryRepo categoryRepo;

        public TestController(ICategoryRepo categoryRepoParam)
        {
            this.categoryRepo = categoryRepoParam;
        }

        public string Get()
        {
            this.categoryRepo.Create();
            return "OK";
        }
    }

So what I found out is it is working for web api, but not working for web project. I am using both in the same project.

like image 995
Wai Yan Hein Avatar asked Jun 19 '16 09:06

Wai Yan Hein


1 Answers

You need to install Ninject.MVC5 and setup the DependencyResolver for MVC along with the DependencyResolver for WebApi

// Web Api
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

// MVC 
System.Web.Mvc.DependencyResolver.SetResolver(new Ninject.Web.Mvc.NinjectDependencyResolver(kernel));
like image 109
jbl Avatar answered Sep 22 '22 08:09

jbl