交叉柱: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);
这种情况偶尔发生,我真的找不到问题的原因。任何建议或信息都将不胜感激。谢谢
我的猜测是,您的resourceItems
变量仍然是从_cacheManager.Get
lambda引用的LINQ可枚举变量。当lambda实际执行时,我怀疑它所引用的对象已经被释放。我建议在lambda中引用enumerable之前先执行它。类似于:
var finalResourceItems = resourceItems.ToList();
return _cacheManager.Get(cacheKey, ctx =>
{
ctx.Monitor(_clock.When(TimeSpan.FromMinutes(CacheTime)));
return finalResourceItems;
});