MVC5,Web API 2和Ninject [英] MVC5, Web API 2 and Ninject

查看:87
本文介绍了MVC5,Web API 2和Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Web API 2创建了一个新的MVC5项目,然后从NuGet添加了Ninject.MVC3包.

I have created a new MVC5 project with Web API 2, I then added the Ninject.MVC3 package from NuGet.

构造函数注入对于MVC5控制器运行良好,但是在尝试将其与Web API控制器一起使用时出现错误.

Constructor injection is working fine for the MVC5 controllers, but i am getting an error when trying to use it with the Web API Controllers.

尝试创建类型为'UserProfileController'的控制器时发生错误.确保控制器具有无参数的公共构造函数.

An error occurred when trying to create a controller of type 'UserProfileController'. Make sure that the controller has a parameterless public constructor.

用于MVC5控制器的构造函数:

Constructor for working MVC5 controller:

public class HomeController : Controller
{
    private IMailService _mail;
    private IRepository _repo;

    public HomeController(IMailService mail, IRepository repo)
    {
        _mail = mail;
        _repo = repo;
    }
}

非正常工作的Web API控制器的构造函数:

Constructor for non-working Web API Controller:

public class UserProfileController : ApiController
{
    private IRepository _repo;

    public UserProfileController(IRepository repo)
    {
        _repo = repo;
    }
}

下面是完整的NinjectWebCommon.cs文件:

Below is the full NinjectWebCommon.cs file:

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

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

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;
using DatingSite.Services;
using DatingSite.Data;

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)
    {
#if DEBUG
        kernel.Bind<IMailService>().To<MockMailService>().InRequestScope();

#else
        kernel.Bind<IMailService>().To<MailService>().InRequestScope();
#endif
        kernel.Bind<SiteContext>().To<SiteContext>().InRequestScope();
        kernel.Bind<IRepository>().To<Repository>().InRequestScope();
    }
}
}

推荐答案

Ninject.Web.WebApi NuGet

The Ninject.Web.WebApi NuGet package has just been released. From now on the preferred solution is using that package. I couldn't find any related documentation but after installing the package everything worked for me.

Install-Package Ninject.Web.WebApi 

安装后,普通的MVC和API控制器实例都由Ninject提供.

After installation both the normal MVC and the API controller instances are provided by Ninject.

如果已经安装了Ninject.Web.Common,请确保从NinjectWebCommon.cs保存绑定,并让Nuget在安装过程中重写NinjectWebCommon.cs,并在完成后放回绑定.

If you have Ninject.Web.Common already installed make sure to save your bindings from NinjectWebCommon.cs and let Nuget rewrite NinjectWebCommon.cs during install and put back your bindings when finished.

正如注释中所指出的,具体取决于您的执行上下文,您还将需要以下软件包中的一个 :

As pointed out in the comments depending on your execution context you will need one of the following packages as well:

  • Ninject.Web.WebApi.WebHost
  • Ninject.Web.WebApi.OwinHost
  • Ninject.Web.WebApi.Selfhost

最常见的情况是使用IIS来选择WebHost程序包.

The most common scenario is IIS for that pick the WebHost package.

如果您从头启动IIS MVC5 Web应用程序并想使用Ninject,请安装以下软件包:

In the case you start an IIS MVC5 web app from scratch and want to use Ninject install the following packages:

  • Ninject -Ninject核心dll
  • Ninject.Web.Common -Ninject的通用Web功能,例如InRequestScope()
  • Ninject.MVC5 -MVC依赖注入器,例如为MVC提供控制器
  • Ninject.Web.Common.WebHost -当IIS启动Web应用程序时,从Ninject.MVC5注册依赖项注入器.如果您不使用IIS,则需要使用其他软件包,请检查上面的内容
  • Ninject.Web.WebApi WebApi依赖注入程序,例如为WebApi提供控制器
  • Ninject.web.WebApi.WebHost -在IIS启动Web应用程序时从Ninject.Web.WebApi注册依赖项注入器.
  • Ninject - Ninject core dll
  • Ninject.Web.Common - Common Web functionality for Ninject eg. InRequestScope()
  • Ninject.MVC5 - MVC dependency injectors eg. to provide Controllers for MVC
  • Ninject.Web.Common.WebHost - Registers the dependency injectors from Ninject.MVC5 when IIS starts the web app. If you are not using IIS you will need a different package, check above
  • Ninject.Web.WebApi WebApi dependency injectors eg. to provide Controllers for WebApi
  • Ninject.web.WebApi.WebHost - Registers the dependency injectors from Ninject.Web.WebApi when IIS starts the web app.

这篇关于MVC5,Web API 2和Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆