本文介绍了ASP.Net MVC3:史蒂芬·桑德森的教程 - 使用Ninject错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目,在Global.asax的Application_Start调用错误: ControllerBuilder.Current.SetControllerFactory(新NinjectControllerFactory());

 命名空间SportsStore.WebUI.Infrastructure
{
    公共类NinjectControllerFactory:DefaultControllerFactory
    {
        私人的iKernel ninjectKernel;        公共NinjectControllerFactory()
        {
            ninjectKernel =新StandardKernel();
            AddBindings();
        }        保护覆盖一个IController GetControllerInstance(RequestContext的RequestContext的,类型controllerType)
        {
            返回controllerType == NULL?空:(一个IController)ninjectKernel.Get(controllerType);
        }        私人无效AddBindings()
        {
            //把额外bindingers这里
        }
    }

错误堆栈:

 定位源'C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs。
校验:MD5 {德1D毫升43 B7 22 44 A5 8D 8B 50版23 DC 4 28}
文件'C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs'不存在。
展望脚本文件为C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs'...
展望项目为C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs。
该文件不是在一个项目中找到。展望目录C:\\ Program Files文件\\微软的Visual Studio 10.0 \\ VC \\ CRT \\ SRC \\'...
展望目录C:\\ Program Files文件\\微软的Visual Studio 10.0 \\ VC \\ atlmfc的\\ src \\ MFC \\'...
展望目录C:\\ Program Files文件\\微软的Visual Studio 10.0 \\ VC \\ atlmfcsrc \\ ATL \\'...
展望目录C:\\ Program Files文件\\微软的Visual Studio 10.0 \\ VC \\ atlmfc \\ \\包括'...
调试器将要求用户查找文件:C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs。
用户pressed在查找源对话框取消。
为有效解决方案的调试源文件设置进行了修改,使调试器将不会要求用户查找文件:C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs。
调试器无法找到源文件C:\\项目\\ Ninject \\ Maintenance2.2 \\ ninject的\\ src \\ Ninject \\基础设施\\ Multimap.cs。


解决方案

我建议使用Ninject MVC3的官方扩展。该包的NuGet将Ninject.MVC3设置你的一切,这样你就可以开始注入depenencies到您的控制器。

更换控制器工厂不是prefered方式进行了MVC3。在这个版本中所提出的计算策略的MVC开发团队是使用DependencyResolver来代替。该Ninject MVC3扩展使用这种方法。

在读取文件https://github.com/ninject/ninject.web.mvc/wiki/MVC3了解更多信息。

My project errors with the global.asax Application_Start calls: ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

namespace SportsStore.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory 
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            // put additional bindingers here
        }
    }

ERROR STACK:

Locating source for 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs'. 
Checksum: MD5 {de 1d cc 43 b7 22 44 a5 8d 8b 50 ed 23 dc 4 28} 
The file 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs' does not exist. 
Looking in script documents for 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs'... 
Looking in the projects for 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs'. 
The file was not found in a project. Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'... 
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'... 
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfcsrc\atl\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'... 
The debugger will ask the user to find the file: c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs. 
The user pressed Cancel in the Find Source dialog. 
The debug source files settings for the active solution have been modified so that the debugger will not ask the user to find the file: c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs. 
The debugger could not locate the source file 'c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Multimap.cs'.
解决方案

I suggest to use the official Ninject MVC3 extension. The NuGet package Ninject.MVC3 will setup everything for you so that you can start to inject depenencies into your controllers.

Replacing the controller factory isn't the prefered way anymore for MVC3. With this release the proposed approch by the MVC development team is to use a DependencyResolver instead. The Ninject MVC3 extension uses this approach.

Read the documentation at https://github.com/ninject/ninject.web.mvc/wiki/MVC3 for more information.

这篇关于ASP.Net MVC3:史蒂芬·桑德森的教程 - 使用Ninject错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:56