Ninject - 无法找到资源

时间:2011-08-15 14:13:31

标签: asp.net-mvc-3 inversion-of-control ninject

我收到错误

  

无法找到资源。

当我尝试在我的MVC-3应用程序中实现Ninject时。在CreateKernel()

期间,问题似乎来自Global.asax
#region Inversion of Control

    protected override IKernel CreateKernel()
    {
        return Container;
    }

    static IKernel _container;
    public static IKernel Container
    {
        get
        {
            if (_container == null)
            {
                _container = new StandardKernel(new SiteModule());
            }
            return _container;
        }
    }

    internal class SiteModule : NinjectModule
    {
        public override void Load()
        {
            bool MOCKDB = true;
            //MOCKDB = false;//Stop Mocking
            if (MOCKDB)
            {
                //Set up mock bindings
                Bind<iItem>().To<LeadServiceMock>();
            }
            else
            {
                //Set up real bindings.
                Bind<iItem>().To<LeadService>();
            }
        }
    }
    #endregion

如果我把上面的代码拿回来并恢复到System.Web.HttpApplication,那么事情就会重新开始。

public class MvcApplication : NinjectHttpApplication//:System.Web.HttpApplication
    {

我从我之前编写的实现中获取此代码,但仍然有效。如果我单步执行调试

protected override IKernel CreateKernel()
{
    return Container;
}

我在两个工作程序中遇到错误,而且这个错误:

Locating source for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'. Checksum: MD5 {b8 b2 52 86 ce 34 de 53 61 76 c9 df ff 65 8c 3f}
The file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs' does not exist.
Looking in script documents for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'...
Looking in the projects for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.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\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs.
The debugger could not locate the source file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'.

我怀疑我在SiteModule做错了。我做错了什么?

1 个答案:

答案 0 :(得分:4)

Application_Start()替换为OnApplicationStarted()

    //protected void Application_Start()
    //{
    //    AreaRegistration.RegisterAllAreas();

    //    RegisterGlobalFilters(GlobalFilters.Filters);
    //    RegisterRoutes(RouteTable.Routes);
    //}

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }