Ninject + MVC3没有注入控制器

Tra*_*raw 14 c# asp.net binding ninject asp.net-mvc-3

我已经使用了NuGet Ninject MVC3扩展,并且根据请求无法将其注入控制器.它似乎没有绑定,因为MVC正在寻找无参数的构造函数.这是堆栈跟踪:

    [MissingMethodException: No parameterless constructor defined for this object.]
       System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
       System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
       System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
       System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

    [InvalidOperationException: An error occurred when trying to create a controller of type 'MyApp.Presentation.Controllers.SearchController'. Make sure that the controller has a parameterless public constructor.]
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
       System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
       System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
       System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +199
       System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
       System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
       System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
       System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
       System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862676
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Run Code Online (Sandbox Code Playgroud)

而我正试图注入的控制器(目前需要一个基类):

public class SearchController : MainBaseController
{

    private readonly MyApp.Domain.Tutor.TutorSearch TutorSearch;
    protected static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    [Ninject.Inject]
    public SearchController(MyApp.Domain.Tutor.TutorSearch tutorSearch)
    {
        this.TutorSearch = tutorSearch;
    }

    .... (no other constructors)
}
Run Code Online (Sandbox Code Playgroud)

和我的NinjectWebCommon.cs的相关部分:

    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)
    {
        INinjectModule[] modules = new INinjectModule[]
        {
            new Domain.DomainModule()
        };

        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Load(modules);

        kernel.Bind<MyApp.Domain.Tutor.TutorSearch>().ToSelf();

        // ***************
        var t = kernel.Get<MyApp.Presentation.Controllers.SearchController>();
    }
Run Code Online (Sandbox Code Playgroud)

最后,这是DomainModule:

public class DomainModule : NinjectModule
{
    public override void Load()
    {
        Bind<Data.Profile.IProfileProvider>().To<Data.Profile.XmlProfileProvider>();
        Bind<Data.Subject.ISubjectProvider>().To<Data.Subject.XmlSubjectProvider>();
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在标有星号的行中的NinjectWebCommon.cs中所看到的,我已经尝试将控制器显式构建为测试.这适用于正确注入的Xml..Providers,效果非常好.

有没有我错过的东西?我不太了解NuGet MVC3扩展的内容,因此不知道控制器绑定在哪个方面失败.

任何关于看什么的指针都将非常感谢,谢谢.

Tra*_*raw 17

经过一段时间的绝望修补,我终于找到了问题所在.我正在引用MVC4(System.Web.Mvc)DLL并且没有将旧版3.0.0.0的绑定重定向到4.0.0.0:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0" newVersion="4.0.0.0" />
    <bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0" />
    <!-- The following line was missing -->
    <bindingRedirect oldVersion="3.0.0.0" newVersion="4.0.0.0" /> 
  </dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

至少,我希望这是一个不错的解决方案.

编辑:如下所示,以下行更简洁:

  <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
Run Code Online (Sandbox Code Playgroud)

  • 不会`bindingRedirect oldVersion ="0.0.0.0-4.0.0.0"newVersion ="4.0.0.0"`在一行中覆盖这三个案例? (7认同)
  • 我真的希望这是在2053年或更早的时候确定的. (4认同)

Ada*_*ent 6

使用Visual Studio和Nuget,您需要运行此命令

安装包Ninject.MVC3

(根据您运行的MVC版本替换3)

这将解决您的问题