Question

Crosspost: https://orchard.codeplex.com/discussions/471475

Sometimes, I get this error: "Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed." when querying some Content Items.

This is my function that builds the items:

        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;
            });
        }

This is part of the function that calls the one above, which at times shows the error:

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

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

It happens sporadically and I can't really find a cause of the issue. Any piece of advise or information would be highly appreciated. Thanks!

Était-ce utile?

La solution

My guess is that your resourceItems variable is still a LINQ enumerable which is referenced from the _cacheManager.Get lambda. When that lambda actually gets executed, I suspect that the objects it refers to have already been disposed. I would recommend executing the enumerable before referencing it in the lambda. Something like:

var finalResourceItems = resourceItems.ToList();

return _cacheManager.Get(cacheKey, ctx =>
{
    ctx.Monitor(_clock.When(TimeSpan.FromMinutes(CacheTime)));
    return finalResourceItems;
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top