坚持通过Steven Sanderson的“ASP.NET MVC 2 ......”

时间:2011-07-19 13:17:46

标签: asp.net-mvc-2 ninject

我在学校收到了对ASP.NET MVC的简要介绍,我正在尝试使用Steven Sanderson的书 Pro ASP.NET MVC 2 Framework 扩展该知识库。这是一个很好的帮助,但我在实现Ninject的示例中遇到了障碍。我相信DI设置正确,但是当我尝试在Ninject控制器类中指定依赖项的绑定时,我会收到两个死亡的棕色屏幕之一:

没有为此对象定义无参数构造函数。

堆栈追踪:

[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.Activator.CreateInstance(Type type) +6
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +491

[InvalidOperationException: An error occurred when trying to create a controller of type 'SportsStore.WebUI.Controllers.ProductsController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +628
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +204
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +193
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +160
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +80
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +45
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8898152
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

未将对象引用设置为对象的实例。

堆栈追踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   SportsStore.WebUI.Infrastructure.SportsStoreServices.Load() in D:\Visual Studio 2010\Projects\CSharp2010AndDotNet4PlatformBook\SportsStore\SportsStore.WebUI\Infrastructure\NinjectControllerFactory.cs:33
   Ninject.Modules.NinjectModule.OnLoad(IKernel kernel) in c:\Projects\Ninject\ninject\src\Ninject\Modules\NinjectModule.cs:60
   Ninject.KernelBase.Load(IEnumerable`1 modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:222
   Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:102
   Ninject.KernelBase..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:57
   Ninject.StandardKernel..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\StandardKernel.cs:31
   SportsStore.WebUI.Infrastructure.NinjectControllerFactory..ctor() in D:\Visual Studio 2010\Projects\CSharp2010AndDotNet4PlatformBook\SportsStore\SportsStore.WebUI\Infrastructure\NinjectControllerFactory.cs:18
   SportsStore.WebUI.MvcApplication.Application_Start() in D:\Visual Studio 2010\Projects\CSharp2010AndDotNet4PlatformBook\SportsStore\SportsStore.WebUI\Global.asax.cs:32

这是我在Ninject控制器类中的代码:

public class NinjectControllerFactory : DefaultControllerFactory
{
    // A Ninject "kernel" is the things that can supply object instances
    private IKernel kernel = new StandardKernel(new SportsStoreServices());

    // ASP.NET MVC calls this to get the controller for each request
    protected override IController GetControllerInstance(RequestContext context, Type controllerType)
    {
        if (controllerType == null)
            return null;
        return (IController)kernel.Get(controllerType);
    }

    // Configures how abstract service types are mapped to concrete implementations
    private class SportsStoreServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IProductsRepository>()
                .To<SqlProductsRepository>()
                .WithConstructorArgument("connectionString",
                    ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString
                    );
        }
    }
}

这本书正在推动我的理解的极限,但我一直在跟随并且能够调试到目前为止的一切。这让我很难过。知道我在哪里开始调试这个吗?

2 个答案:

答案 0 :(得分:2)

这两个问题都与Ninject未正确设置或集成有关。 Ninject的主干版本允许您绕过必须定义自定义控制器工厂。这是一个更清洁的设置,更少混淆。

请参阅:

此外,您应该查看此处列出的文章

答案 1 :(得分:1)

您是否在global.asax中更新了application_start以指向ninject控制器工厂?

因此:

protected void Application_Start() {
..........

  ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory ());
}