title: Custom action filter attribute performs poorly when compared to RequestSizeLimit attribute
body: <p>I have a use-case when I need to limit the allowed request size for the file upload WebAPI endpoint. Normally I would use the in-built attribute <code>RequestSizeLimit</code>:</p>
  <pre><code>    [HttpPost]
      [RequestSizeLimit(104_857_600)] // 100mb limit
      public async Task&lt;IActionResult&gt; Upload([FromForm] IFormFile file) {
  
      }
  </code></pre>
  <p>However, per requirements I need to make the request size limit to be configurable and shared with rest of the application (for example with SPA application which uses the same value for it's own validation before user uploads the file, also with unit-tests etc..). So to cover this use-case I have implemented my own action filter:</p>
  <pre><code>public class CustomRequestSizeValidatorAttribute : ActionFilterAttribute
  {
      public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
      {
          var request = context.HttpContext.Request;
          var contentLength = request.ContentLength;
  
          if (contentLength &gt; SharedConsts.MaximumFileUploadSize)
          {
              var errorMessage = &quot;Request content length is too large&quot;;
              context.Result = new BadRequestObjectResult(errorMessage);
          }
          else
          {
              await next();
          }
      }
  }
  </code></pre>
  <p>And:</p>
  <pre><code>public static class SharedConsts
  {
      public static readonly long MaximumFileUploadSize = 104_857_600; // 100 mb
  }
  </code></pre>
  <p>And use it as:</p>
  <pre><code>    [HttpPost]
      [CustomRequestSizeValidator]
      public async Task&lt;IActionResult&gt; Upload([FromForm] IFormFile file) {
  
      }
  </code></pre>
  <p>And here comes the problem. I upload the <strong>180mb</strong> file via <code>Postman</code> and here are the service response times:</p>
  <ul>
  <li>without any of the 2 attributes ~ <strong>2.5 - 3 seconds</strong></li>
  <li>with <code>[CustomRequestSizeValidator]</code> ~ <strong>same time (2.5 - 3 seconds)</strong></li>
  <li>with <code>[RequestSizeLimit]</code> ~ <strong>22ms</strong> !</li>
  </ul>
  <p>Why is my custom attribute <code>[CustomRequestSizeValidator]</code> so slow, as well as how to make the request execute just as fast as with the native attribute from MS <code>[RequestSizeLimit]</code>?</p>
  <p>From the diagnostic tools I see that when <code>[CustomRequestSizeValidator]</code> attribute is used - memory consumption grows every time the request is made. With <code>[RequestSizeLimit]</code> attribute - memory never grows. So the problem is probably the Order in which logic is executed by the framework. My custom attribute is most likely executed as the very last step in the pipeline.. But I still have no idea how to fix it so any advices are welcomed.</p>