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

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

问题描述

更新 - 请查看我的回答以获取此问题解决方案的链接和说明

在我们开始之前,我知道这是一个非常常见的问题,我已经在许多卫星上使用 Ninject 没有问题,但现在出现了,我想不出解决办法.另外,不,到目前为止,Google 和 SO 上的所有结果都没有帮助我.

因此,请考虑在 Windows Server 2008 R2 上的 Visual Studio 2012 的非常、非常、非常简单的原型 ASP.NET MVC 4 项目上运行以下代码:

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

现在,这就是我使用 Ninject 的其他项目(但在 .NET 4 上是 ASP.NET MVC 3)的工作方式,据我所知,这是使一切正常工作所需要的.那么,为什么我会突然收到没有为此对象定义的无参数构造函数. 异常?

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.我在 2012 年的同一台机器上安装了 Visual Studio 2010,为 2010 年安装了 ASP.NET MVC 4,然后我将 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 调试它的项目时,它只是出现了No parameterless constructor defined for this object 异常.2012 年在 .NET 4.0 和 .NET 4.5 之间重新定位没有任何作用.从 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 问题中找到了解决方案: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. Up-vote the snot out of that post I linked because it deserves it!

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

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