ASP.NET MVC 2,Ninject 2.2和无参数的构造函数此对象定义 [英] ASP.NET MVC 2, Ninject 2.2 and no parameterless constructor defined for this object

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

问题描述

所以,我已经花了一些时间与ASP.NET MVC 2(目前坚持使用Visual Studio 2008),现在已经走上了使用Ninject 2.2及其MVC整合。我从以下位置下载Ninject 2.2和Ninject.Web.Mvc:

So I've been spending some time with ASP.NET MVC 2 (currently stuck with using Visual Studio 2008) and have now moved onto using Ninject 2.2 and its MVC integration. I've downloaded Ninject 2.2 and Ninject.Web.Mvc from the following locations:

https://github.com/downloads/ninject/ninject/Ninject-2.2.0.0-release-net-3.5.zip

<一href=\"https://github.com/downloads/ninject/ninject.web.mvc/Ninject.Web.Mvc2-2.2.0.0-release-net-3.5.zip\" rel=\"nofollow\">https://github.com/downloads/ninject/ninject.web.mvc/Ninject.Web.Mvc2-2.2.0.0-release-net-3.5.zip

在我的MVC 2项目中引用它们。我的Global.asax.cs文件看起来像这样(pretty许多Ninject.Web.Mvc自述说什么):

And referenced them in my MVC 2 project. My Global.asax.cs file looks like this (pretty much what the Ninject.Web.Mvc README says):

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Mvc;
using Ninject;

namespace Mvc2 {
    public class MvcApplication : NinjectHttpApplication {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected override void OnApplicationStarted() {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel() {
            var kernel = new StandardKernel();

            kernel.Bind<IFoo>().To<Foo>();

            return kernel;
        }
    }
}

和一个家居控制器,看起来像这样:

And a home controller that looks like this:

using System;
using System.Web;
using System.Web.Mvc;

namespace Mvc2.Controllers {
    public class HomeController : Controller {
        private readonly IFoo foo;

        public HomeController(IFoo foo) {
            this.foo = foo;
        }

        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }
}

现在每次我跑我的项目,并参观/我得到的死亡黄屏,上面写着一个消息时间这个对象定义无参数的构造函数。这似乎Ninject不解决我的Foo服务,其注入的HomeController。我想我失去了一些东西真的很明显,但我只是没有看到它。

Now every time I run my project and visit '/' I get a yellow screen of death with a message that says "No parameterless constructor defined for this object." It seems Ninject is not resolving my Foo service and injecting it into HomeController. I imagine I'm missing something really obvious but I'm just not seeing it.

我如何Ninject注入到富的HomeController中,并没有使用Ninject属性?

How do I get Ninject to inject Foo into the HomeController, and without using Ninject attributes?

推荐答案

的:你能否提供有关IFoo的服务实现一点点的更多信息?它有自己所有的依赖是否满意?

Me: Could you provide a little more information about the IFoo service implementation? Does it have all of its own dependencies satisfied?

我自己的:嗯,没有没有。原来我没有约束力的依赖。男孩,是错误消息和堆栈跟踪误导!

Myself: Hmm, no it doesn't. Turns out I didn't bind its dependencies. Boy, is that error message and stack trace misleading!

所以我的错误是,我并没有绑定的IFoo实现的依赖关系之一,并因此Ninject默默地失败,并试图继续其快乐的方式。这实在是不幸的,因为它可能会导致一些非常奇怪的行为,一旦我从一个简单的设置偏差。我想我的下一个问题应该是怎样才能得到Ninject为尽早失败,并提供了有关什么是错的好消息?但现在我至少可以得到它。

So my mistake was that I didn't bind one of the dependencies of the IFoo implementation and so Ninject failed silently and tried to continue on its merry way. Which is really unfortunate because it could lead to some really strange behavior once I deviate from a trivial setup. I guess my next question should be how can I get Ninject to fail as early as possible and provide good messaging about what's wrong? But for now I can at least get on with it.

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

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