【问题标题】:Sporadic LifetimeScope issues零星的 LifetimeScope 问题
【发布时间】:2013-11-29 06:54:20
【问题描述】:

转帖:https://orchard.codeplex.com/discussions/471475

有时,我会收到此错误: “无法解析实例,并且无法从此 LifetimeScope 创建嵌套生命周期,因为它已被释放。” 查询某些内容项时。

这是我构建项目的函数:

        protected IEnumerable<ContentItem> GetResources(string[] productFilters, string[] topicFilters, string[] techLevelFilters)
        {                  
            var cacheKey = BuildCacheKey(productFilters, topicFilters, techLevelFilters);
            var resourceItems = ContentManager.Query().ForType("Resource").ForVersion(VersionOptions.Published).List();

            if (productFilters != null && productFilters.Any())
            {
                resourceItems = resourceItems.Where(r => productFilters.Contains(GetTaxonomyFieldValue(r, productTaxonomy)));
            }

            if (topicFilters != null && topicFilters.Any())
            {
                resourceItems = resourceItems.Where(r => topicFilters.Contains(GetTaxonomyFieldValue(r, topicTaxonomy)));
            }

            if (techLevelFilters != null && techLevelFilters.Any())
            {
                resourceItems = resourceItems.Where(r => techLevelFilters.Contains(GetTaxonomyFieldValue(r, techLevelTaxonomy)));
            }

            return _cacheManager.Get(cacheKey, ctx =>
            {
                ctx.Monitor(_clock.When(TimeSpan.FromMinutes(CacheTime)));
                return resourceItems;
            });
        }

这是调用上述函数的函数的一部分,有时会显示错误:

            string[] newProductFilters = GetProductFilters(0);
            if (newProductFilters != null && newProductFilters.Any())
            {
                productFilters = productFilters.Concat(newProductFilters).ToArray();
            }     

            var resourceItems = GetResources(productFilters, topicFilters, techLevelFilters);

它偶尔发生,我无法真正找到问题的原因。任何建议或信息将不胜感激。谢谢!

【问题讨论】:

  • 完全不清楚您是如何处理依赖项的。你如何注射它们?你直接使用lifescope吗?您能否发布更多代码,请对错误进行堆栈跟踪。

标签: asp.net-mvc-4 orchardcms autofac orchardcms-1.6 lifetime-scoping


【解决方案1】:

我的猜测是您的 resourceItems 变量仍然是一个 LINQ 可枚举,它是从 _cacheManager.Get lambda 引用的。当该 lambda 实际执行时,我怀疑它所指的对象已经被释放。我建议在 lambda 中引用它之前执行可枚举。比如:

var finalResourceItems = resourceItems.ToList();

return _cacheManager.Get(cacheKey, ctx =>
{
    ctx.Monitor(_clock.When(TimeSpan.FromMinutes(CacheTime)));
    return finalResourceItems;
});

【讨论】:

  • 我想是的。但我会这样写 pastebin.com/pXidWHs7 。将整个方法封装在 Cache Get 中。
  • @martinbc 这也可以,只要记住在这种情况下也要执行可枚举(`return resourceItems.ToList();),因为你仍然会遇到同样的问题
  • 这正是我编写 Orchard 缓存方法的方式。它对我有用。
  • 太棒了! .ToList() 拯救了我的一天!
猜你喜欢
  • 2019-11-29
  • 1970-01-01
  • 2017-10-19
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2023-03-08
  • 2015-01-20
相关资源
最近更新 更多