Caching Randomly throws InvalidOperationException

gregf's Avatar


12 Jun, 2018 01:54 PM

We are currently using PostSharp Caching throughout our program recently we've noticed randomly an InvalidOperationException is occurring during the caching process. The error message reads "Cannot add a dependency to a context that has already been closed." This does not appear to be a Windows generated error message but I also can't find it in the Postsharp knowledge base either but every time it occurs it throws from the Postsharp library. I was hoping someone in support could give some insight into this and why it might be occurring, as I'm kind of poking around in the dark with this at the moment and can't make much sense of it.

Call stack example:

 at PostSharp.Patterns.Caching.CachingContext.AddDependency (PostSharp.Patterns.Caching, Version=, Culture=neutral, PublicKeyToken=e7f631e6ce13f078) 
at PostSharp.Patterns.Caching.CachingContext.AddDependency (PostSharp.Patterns.Caching, Version=, Culture=neutral, PublicKeyToken=e7f631e6ce13f078) 
at PostSharp.Patterns.Caching.Implementation.CachingFrontend.AddCacheHitDependencies (PostSharp.Patterns.Caching, Version=, Culture=neutral, PublicKeyToken=e7f631e6ce13f078) 
at PostSharp.Patterns.Caching.Implementation.CachingFrontend.GetOrAdd (PostSharp.Patterns.Caching, Version=, Culture=neutral, PublicKeyToken=e7f631e6ce13f078) 
at PostSharp.Patterns.Caching.CacheAttribute.OnInvoke (PostSharp.Patterns.Caching, Version=, Culture=neutral, PublicKeyToken=e7f631e6ce13f078) 
at ABELSoft.Dental.Application.Managers.ProviderManager.GetProviderName
  1. Support Staff 1 Posted by PostSharp Techn... on 12 Jun, 2018 02:51 PM

    PostSharp Technologies's Avatar

    Hi Greg,

    Thank you for trying caching.

    The exception is an internal one; most probably you're doing nothing wrong and this is a bug.
    It seems it happens when a cached method is invoking another cache method, which is a supported scenario, but somehow some untested condition is happening there.

    Is it possible that GetProviderName is (directly or indirectly) invoked from a cached async method? That would explain the randomness of the issue. Please confirm this hypothesis.

    Another way to move forward is to enable logging of caching. Install the PostSharp.Patterns.Diagnostics package (plus one relevant back-end package) and enable logging using this code:

    LoggingServices.DefaultBackend = new ConsoleLoggingBackend(); // Replace with the relevant backend
    See for details about configuring logging (remember that you need to configure the `Caching` role, not the default one).
    Then you can send us the log.
  2. Support Staff 2 Posted by PostSharp Techn... on 27 Jun, 2018 04:11 PM

    PostSharp Technologies's Avatar


    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    PostSharp Team

  3. PostSharp Technologies closed this discussion on 27 Jun, 2018 04:11 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac