Crosspost: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);
它偶尔发生,我无法找到问题的原因。任何建议或信息都将受到高度赞赏。谢谢!
答案 0 :(得分:3)
我的猜测是你的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;
});