Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在MVC应用程序中重置Ninject的IKernel容器的最佳方法是什么?_.net_Asp.net Mvc 2_Dependency Injection_Ninject_Ninject 2 - Fatal编程技术网

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

.net 在MVC应用程序中重置Ninject的IKernel容器的最佳方法是什么?,.net,asp.net-mvc-2,dependency-injection,ninject,ninject-2,.net,Asp.net Mvc 2,Dependency Injection,Ninject,Ninject 2,基本上,在我的Global.asax代码中,我为Ninject设置了以下IKernel属性,类似这样的设置还利用了Microsoft.Practices.ServiceLocation。一旦该容器出现在CreateKernel覆盖上,就会自动调用它: protected override IKernel CreateKernel() { return Container; } 和我的容器属性: static IKernel _contai

基本上,在我的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;
            }
        }
正如你所看到的,我只加载了一个模块,定义了我的interfaceservice绑定列表,但这对这个问题并不重要,但我的问题是——无论我多么努力地尝试,我都无法在重新启动MVC网站时将我的_容器初始化为空。从编辑和重新保存Web.config文件到刷新应用程序池,甚至重新启动IIS!,我的容器显然仍然存在。我真不明白怎么会是这样。我知道在初始加载时,容器为空,SiteModule确实正确加载

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

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


谢谢各位,如果你们觉得需要查看SiteModule,请告诉我,我可以用代码扩展这篇文章。

你们试过重新启动web服务器吗?我意识到这在生产中可能不是一个选项,但如果可行的话,它应该可以帮助您完成开发阶段。

您尝试过重新启动web服务器吗?我意识到这在生产环境中可能不是一个选项,但如果可行的话,它应该可以帮助您完成开发阶段。

如果我没有弄错的话,CreateKernel在应用程序启动期间只调用一次,所以,除非您在其他地方使用容器,否则缓存它有什么好处吗

你试过这样的东西吗

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

    // Do your Load() and ServiceLocator stuff here

    return kernel;
}
作为参考,Ninject网站的。

如果我没有弄错的话,CreateKernel在应用程序启动时只被调用一次,因此,除非您在其他地方使用容器,否则缓存它有什么好处吗

你试过这样的东西吗

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

    // Do your Load() and ServiceLocator stuff here

    return kernel;
}

作为参考,Ninject网站的。

正如我所说,我尝试重新启动IIS,我没有尝试重新启动整个服务器=正如我所说,我尝试重新启动IIS,我没有尝试重新启动整个服务器=谢谢伙计,我会看一看。谢谢伙计,我会看一看。