在 MVC 应用程序中重置 Ninject 的 IKernel 容器的最佳方法是什么?

发布于 2024-09-02 19:07:07 字数 1321 浏览 5 评论 0原文

基本上在我的 Global.asax 代码中,我有以下用于 Ninject 设置的 IKernel 属性(还利用了 Microsoft.Practices.ServiceLocation)。一旦出现在 CreateKernel() 覆盖中,就会自动调用此容器:

protected override IKernel CreateKernel()
        {
            return Container;
        }

以及我的容器属性:

static IKernel _container;
        public static IKernel Container
        {
            get
            {
                if (_container == null)
                {
                    _container = new StandardKernel();
                    _container.Load(new SiteModule(_container));
                    ServiceLocator.SetLocatorProvider(() => _container.Get<IServiceLocator>());
                }
                return _container;
            }
        }

如您所见,我仅加载一个定义我的接口<->服务绑定列表的模块,这对这个问题来说并不重要,但是我的问题是 - 无论我看起来多么努力,当我重新启动时,一旦它被初始实例化,我就无法再次获得我的 _container null我的 MVC 网站。从编辑和重新保存 Web.config 文件(好老技巧)到刷新应用程序池甚至重新启动 IIS(!),我的容器显然仍然存在。我实在不明白怎么会这样。我知道在我的初始加载中 _container 为 null 并且 SiteModule 确实加载正确。

这当然是一个问题,因为我现在希望为新创建的服务添加一些新的绑定,并且容器永远不会返回 null :P

FYI。即使将我的断点移动到容器空测试中似乎也无法解决这个问题,不要问我这如何不能解决问题,但我知道 if 的内部应该是有效的,因为在初始加载时有没有错误,一切都映射得很好。

谢谢大家,如果您觉得需要查看 SiteModule() 请告诉我,我可以用代码扩展这篇文章。

Basically in my Global.asax code I have the following IKernel property for Ninject setup like this (Also taking advantage of Microsoft.Practices.ServiceLocation). This Container is automatically called upon once it seems on the CreateKernel() override:

protected override IKernel CreateKernel()
        {
            return Container;
        }

and my container property:

static IKernel _container;
        public static IKernel Container
        {
            get
            {
                if (_container == null)
                {
                    _container = new StandardKernel();
                    _container.Load(new SiteModule(_container));
                    ServiceLocator.SetLocatorProvider(() => _container.Get<IServiceLocator>());
                }
                return _container;
            }
        }

As you can see I am loading simply one module defining a list of my interface<->service bindings, which shouldn't be important to this problem however, but my problem being - no matter how hard I seem to try, I cannot get my _container null again once it has been initially instantiated when I restart my MVC website. From editing and re-saving the Web.config file (good old trick) through to flushing the app pool or even restarting IIS(!), my container apparently still exists. I really don't understand how this can be the case. I know on my initial loads _container is null and SiteModule does load correctly.

This is a problem of course because I now wish to add some new bindings for newly created services and the container is never returning to null :P

FYI. Even moving my breakpoint into the container null test doesn't appear to get this going, don't ask me how this doesn't fix the issue, but I know the inside of that if should be valid, because on initial load there are no errors, everything maps up fine.

Thanks guys, if you feel you need to see SiteModule() let me know and I can extend this post with the code.

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

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

发布评论

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

评论(2

昵称有卵用 2024-09-09 19:07:07

您是否尝试过重新启动网络服务器?我意识到这可能不是生产中的一个选项,但它应该可以帮助您完成开发阶段(如果它有效)。

Have you tried restarting your web server? I realize this might not be an option in production, but it should get you through the development phase (if it works).

且行且努力 2024-09-09 19:07:07

如果我没记错的话, CreateKernel()Application_Start() 期间仅调用一次(查看源代码),除非您使用的是 Container 在其他地方,缓存它有什么好处吗?

你尝试过这样的事情吗?

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

    // Do your Load() and ServiceLocator stuff here

    return kernel;
}

作为参考,Ninject 网站的 实施

If I am not mistaken, CreateKernel() is only called once during Application_Start() (view source) so, unless you are using Container elsewhere, is there any benefit to caching it?

Have you tried something like this?

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

    // Do your Load() and ServiceLocator stuff here

    return kernel;
}

For reference, the Ninject website's implementation.

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