本文介绍了使用ASP.Net MVC 4的Web API与Ninject.Web.WebApi问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用与Ninject新的ASP.Net MVC 4的Web API项目模板,但都打在下面的错误墙:

I'm trying to use the new ASP.Net MVC 4 Web API project template with Ninject but have hit a wall on the following error:

方法'GetFilters'类型'Ninject.Web.WebApi.Filter.DefaultFilterProvider从程序集Ninject.Web.WebApi,版本= 3.0.0.0,文化=中性公钥= c7192dc5380945e7没有实现。

我创建Visual Studio 2010中使用ASP.Net MVC 4一个全新的项目 - >网络API模板,我使用的是最新的Ninject的NuGet包:

I am creating a brand new project in Visual Studio 2010 using the ASP.Net MVC 4 -> Web API template and I am using the latest Ninject NuGet packages:


  • Ninject 3.0.1.10

  • Ninject.Web.Common 3.0.0.7

  • Ninject.Web.WebApi 3.0.0.2

我试图然而,在我已经没有任何运气 - 如果我删除提及Ninject.Web.WebApi然后MVC从未从事Ninject。我还发现他们提到的 Ninject.MVC3 但是我现在用新的 Ninject.WebApi 插件。

I have attempted the solution presented in this question however I've not had any luck - if I remove the reference to Ninject.Web.WebApi then MVC never engages Ninject. I also notice they mention Ninject.MVC3 however I am using the new Ninject.WebApi plugin.

我使用的是默认的 NinjectWebCommon.cs 那是的NuGet期间创建的安装,并试图在 RegisterServices注册一个简单的服务()

I am using the default binding code in NinjectWebCommon.cs that is created during the NuGet install and attempting to register one simple service in RegisterServices()

[assembly: WebActivator.PreApplicationStartMethod(typeof(mkts.web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(mkts.web.App_Start.NinjectWebCommon), "Stop")]

namespace mkts.web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using mkts.service;

    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)
        {
            //Test binding
            kernel.Bind<IStudentService>().To<StudentService>();
        }
    }
}

我的控制器:

namespace mkts.web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IStudentService studentService;

        public HomeController(IStudentService studentService)
        {
            this.studentService = studentService;
        }


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

提前许多感谢任何帮助。

Many thanks in advance for any help with this.

推荐答案

Ninject.WebApi写对测试版,现在pcated德$ P $。

Ninject.WebApi was written against the Beta and is deprecated now.

删除,安装Ninject.MVC3。

Remove that, install Ninject.MVC3.

现在,您将需要一个家酿的IDependencyResolver和IDependencyScope。我在这里发表演练 - http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

Now you will need a homebrewed IDependencyResolver and IDependencyScope. I posted a walkthrough here - http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

这篇关于使用ASP.Net MVC 4的Web API与Ninject.Web.WebApi问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:33