title: MVC Action Filter and Multiple Threads
body: <p>I'm currently having what i think is a threading issue with action filters, on my app i'm using an  ActionFilter to perform tracing of each action, this trace will provide statistical information such as the duration of the call and also log the parameters being sent to the action.</p>
  
  <p>The actual trace implementation (which was done by other team) works with an IDisposable object, basically when creating the instance initializes the start time, and when disposing the object sets the end date, both calls create an entry in a custom log,  the code is bellow (remove some code for simplicity purposes):</p>
  
  <pre><code>    public class TraceActionAttribute : ActionFilterAttribute
  {
      private IDisposable logManagerBeginTrace;
  
      /// &lt;summary&gt;
      /// Called by the ASP.NET MVC framework before the action method executes.
      /// &lt;/summary&gt;
      /// &lt;param name="filterContext"&gt;The filter context.&lt;/param&gt;
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          base.OnActionExecuting(filterContext);
          List&lt;object&gt; parameters = new List&lt;object&gt;();
  
          string actionName = filterContext.ActionDescriptor.ActionName;
          Type controllerType = filterContext.Controller.GetType();
          foreach (KeyValuePair&lt;string, object&gt; currentParameter in filterContext.ActionParameters)
          {
              parameters.Add(currentParameter.Value);
          }
  
          this.logManagerBeginTrace = LogManager.BeginMethodTrace(ApplicationConstants.ApplicationName, controllerType, actionName, Guid.NewGuid(), parameters.ToArray());
      }
  
      /// &lt;summary&gt;
      /// Called by the ASP.NET MVC framework after the action method executes.
      /// &lt;/summary&gt;
      /// &lt;param name="filterContext"&gt;The filter context.&lt;/param&gt;
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
          base.OnActionExecuted(filterContext);
          this.logManagerBeginTrace.Dispose();
      }
  }
  </code></pre>
  
  <p>The exception is not telling me much, basically that it is trying to dispose elements while others are still active, i still have to look into the tracer code... but i found <a href="https://stackoverflow.com/questions/5256058/httpcontext-in-mvc-attributes-threading-issues">this post</a> which says the following:</p>
  
  <blockquote>
    <p>In previous versions of ASP.NET MVC, action filters were created per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.</p>
  </blockquote>
  
  <p>Which for me appears very weirds, since action filters should be peer request, that's the reason we place public properties on it, and configure its behavior for specific Actions, don't we?</p>
  
  <p>I appreciate any help on this, Regards.</p>