Ninject - 找不到资源

发布于 2024-11-29 12:21:49 字数 2777 浏览 0 评论 0原文

我收到错误

找不到资源。

当我尝试在我的 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 中做错了什么。我做错了什么?

I get error

The resource cannot be found.

When I try to implement Ninject in my MVC-3 application. The problem appears to be coming from Global.asax during CreateKernel()

#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

If I take the code above out and revert back to System.Web.HttpApplication then things start to work again.

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

I took this code from a previous implementation that I wrote that also still works. If I step through debug

protected override IKernel CreateKernel()
{
    return Container;
}

I get an error in both the working program and this broken one:

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'.

I suspect I did someting wrong in SiteModule. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2024-12-06 12:21:49

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

Replace Application_Start() with 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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文