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

查看:29
本文介绍了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 

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

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天全站免登陆