ASP.NET MVC 4 + Ninject MVC 3 =此对象定义无参数的构造函数 [英] ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

查看:538
本文介绍了ASP.NET MVC 4 + Ninject MVC 3 =此对象定义无参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 - 请大家看我的回答对解决方案的链接,并解释这个问题

在我们开始之前,我知道这是一个非常普遍的问题,我一直在使用Ninject对许多卫星没有问题,但现在它来了,我想不通的修复程序。此外,没有,没有在谷歌的结果,所以,至今已帮助了我。

所以,考虑一个非常,非常,非常简单的原型ASP.NET MVC 4从Visual Studio 2012在Windows Server 2008 R2的项目运行code以下位:

So, consider the following bit of code running on a very, very, very simple prototype ASP.NET MVC 4 project from Visual Studio 2012 on Windows Server 2008 R2:

public class DefaultController : Controller {
    private IGroupPrincipalRepository GroupPrincipalRepository { get; set; }

    [Inject]
    public DefaultController(
        IGroupPrincipalRepository groupPrincipalRepository) {
        this.GroupPrincipalRepository = groupPrincipalRepository;
    }
}

和这里的 NinjectWebCommon.cs RegisterServices 方法:

kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(
    c =>
        new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "?", "?", "?", "?"))).InSingletonScope();

现在,这是我认为(在.NET 4中,但ASP.NET MVC 3)工作中使用Ninject而据我所知,这是使一切工作需要些什么其他项目怎么样。那么,为什么我突然得到的无参数的构造函数此对象定义。的异常?

Now, this is how my other projects that use Ninject (but are ASP.NET MVC 3 on .NET 4) work and as far as I know this is what's needed to make everything work. So, why am I suddenly getting No parameterless constructor defined for this object. exceptions?

更新

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

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

namespace App_Start {
    using System;
    using System.DirectoryServices.AccountManagement;
    using System.Repositories.ActiveDirectory;
    using System.Web;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        public static void Start() {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        public static void Stop() {
            bootstrapper.ShutDown();
        }

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

        private static void RegisterServices(
            IKernel kernel) {
            kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(
                c =>
                    new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "", "", "", ""))).InSingletonScope();
        }
    }
}

更新 - 请大家看我的回答对解决方案的链接,并解释这个问题

推荐答案

好吧,我没有一个确切的答案的为什么的错误是来了,但我知道的的原因造成的,这是Visual Studio的2012年。我安装了Visual Studio 2010的同一台机器上2012,安装ASP.NET MVC 4,2010年,我重新创建项目2012至2010年一个字一个字,字母信。最终的结果是,当2010调试项目的一切工作正常,Ninject注入的依赖,因为它应该。

Well, I don't have an exact answer why the error is coming up, but I do know who is causing it and that is Visual Studio 2012. I installed Visual Studio 2010 on the same machine as 2012, installed ASP.NET MVC 4 for 2010 and I recreated the 2012 project into 2010 word for word, letter for letter. The final result is that when 2010 debugs the project everything works fine and Ninject injects the dependencies as it should.

在2012调试其项目只是与无参数此对象例外定义的构造函数来了。重新定位.NET 4.0和.NET 4.5在2012年之间不会做任何事情。重新安装从的NuGet Ninject也没有做任何事情。我甚至配置2010年和2012项目使用本地IIS服务器,以绝对确保和最终的结果是一样的。

When 2012 debugs its project it just comes up with the No parameterless constructor defined for this object exception. Re-targeting between .NET 4.0 and .NET 4.5 in 2012 doesn't do anything. Re-installing Ninject from NuGet also doesn't do anything. I even configured both 2010 and 2012 projects to use the local IIS server to be absolutely sure and the end result is the same.

我要假定有与Visual Studio 2012或Ninject的错误。我已经在两个项目之间有唯一的区别是,他们是从运行的IDE和2012年项目是的崩溃所以这就是为什么我指着的Visual Studio 2012的手指之一。

I'm going to assume that there's a bug with Visual Studio 2012 or with Ninject. The only difference I've got between the two projects is which IDE they're running from and the 2012 project is the one that's crashing so that's why I'm pointing the finger at Visual Studio 2012.

更新

家伙。家伙!我遇到了这个问题又来了,发现在另一个SO问题的解决方案:<一href=\"http://stackoverflow.com/questions/14343388/ninject-mvc3-is-not-injecting-into-controller\">Ninject + MVC3不注入控制器。

Guys. GUYS! I ran into this problem AGAIN, and found the solution in another SO question: Ninject + MVC3 is not injecting into controller.

基本上,这是什么从Web.config,这使得它的工作丢失:

Basically, this is what's missing from the Web.config which makes it work:

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

我猜这迫使框架要注意的IoC容器,它允许Ninject最终能够绑定。虽然,我不禁觉得Ninject的NuGet包应该寻找的是存在在Web.config绑定重定向和自动神奇地添加它。这肯定会帮助很多头发拉发生过这个问题。

I'm guessing this forces the framework to be aware of IoC containers which allows Ninject the finally be able to bind. Although, I can't help but think that the Ninject NuGet package should look for the existence of that binding redirect in the Web.config and auto-magically add it. It sure would help with a lot of hair pulling happening over this issue.

P.S。截至票鼻涕了那个帖子我联系,因为它应得的!的

P.S. Up-vote the snot out of that post I linked because it deserves it!

这篇关于ASP.NET MVC 4 + Ninject MVC 3 =此对象定义无参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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