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

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

问题描述

我已创建的Web API 2新MVC5项目,我再从的NuGet添加Ninject.MVC3包。

构造方法注入工作正常的MVC5控制器,但试图与Web API控制器使用它时,我收到一个错误。


  

努力创造型UserProfileController的控制器时发生错误。确保控制器具有一个无参数公共构造函数。


构造工作MVC5控制器:

公共类HomeController的:控制器
{
    私人IMailService _mail;
    私人IRepository _repo;    公众的HomeController(IMailService邮件,IRepository回购)
    {
        _mail =邮件;
        _repo =回购;
    }
}

构造非工作的Web API控制器:

公共类UserProfileController:ApiController
{
    私人IRepository _repo;    公共UserProfileController(IRepository回购)
    {
        _repo =回购;
    }
}

以下是完整NinjectWebCommon.cs文件:

[装配:WebActivator preApplicationStartMethod(typeof运算(DatingSite.App_Start.NinjectWebCommon),开始)
[大会:WebActivator.ApplicationShutdownMethodAttribute(typeof运算(DatingSite.App_Start.NinjectWebCommon),停止)]命名空间DatingSite.App_Start
{
使用系统;
使用的System.Web;使用Microsoft.Web.Infrastructure.DynamicModuleHelper;使用Ninject;
使用Ninject.Web.Common;
使用DatingSite.Services;
使用DatingSite.Data;公共静态类NinjectWebCommon
{
    私人静态只读引导程序引导程序=新的引导程序();    ///<总结>
    ///启动应用
    ///< /总结>
    公共静态无效的start()
    {
        DynamicModuleUtility.RegisterModule(typeof运算(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof运算(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }    ///<总结>
    ///停止应用程序。
    ///< /总结>
    公共静态无效停止()
    {
        bootstrapper.ShutDown();
    }    ///<总结>
    ///创建将管理应用程序的内核。
    ///< /总结>
    ///<收益方式>创建的内核和LT; /回报>
    私有静态的iKernel CreateKernel()
    {
        VAR内核=新StandardKernel();
        kernel.Bind<&Func键LT;的iKernel>方式>()ToMethod(CTX =>()=>新建引导程序()内核。);
        kernel.Bind&所述; IHttpModule的方式>()到< HttpApplicationInitializationHttpModule>();        RegisterServices(内核);
        返回内核;
    }    ///<总结>
    ///装入模块或在这里注册您服务!
    ///< /总结>
    ///< PARAM NAME =内核方式>内核< /参数>
    私有静态无效RegisterServices(内核的iKernel)
    {
#如果DEBUG
        kernel.Bind&所述; IMailService方式>()到< MockMailService方式>()InRequestScope();#其他
        kernel.Bind&所述; IMailService方式>()到< MailService的方式>()InRequestScope();
#万一
        kernel.Bind&所述; SiteContext方式>()到< SiteContext方式>()InRequestScope();
        kernel.Bind&所述; IRepository方式>()到<库方式>()InRequestScope();
    }
}
}


解决方案

该Ninject.Web.WebApi的NuGet 刚刚被释放。从现在的preferred解决方案是使用这个包。
我找不到任何相关文件,但在安装包后一切为我工作。

 安装封装Ninject.Web.WebApi

安装后都正常MVC和API控制器实例由Ninject提供的。

如果您已经安装Ninject.Web.Common确保从NinjectWebCommon.cs保存您的绑定,让重写的NuGet安装过程中NinjectWebCommon.cs,完成后放回你的绑定。

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

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.

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

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

Constructor for non-working Web API Controller:

public class UserProfileController : ApiController
{
    private IRepository _repo;

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

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

解决方案

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 

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

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.

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

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