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

℡╲_俬逩灬. 提交于 2021-02-18 07:06:20

问题


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

Then I installed Ninject.Web using nuget package manager

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/37905713/using-ninject-with-ninject-web-api-for-web-api-2-is-not-working-in-asp-net-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!