Using ASP.NET MVC4 I have created a DelegatingHandler in a WebAPI project. I use the Handler to insert some data into the HttpRequestMessage Properties collection so that later portions of the Http pipeline (e.g. other handlers, controllers) can access that data.
Example:
var httpRequest = (HttpRequestMessageProperty) request.Properties["httpRequest"];
httpRequest.Headers["MY_DATA"] = "some data";
I wanted to test the impact of this on the throughput of WebAPI request processing so I created a console app that, within an Action delegate, instantiates an HttpClient, sends a request, and waits for the response before sending the next one. I count the number of successful executions of this Action delegate within a 5 second window using the performance measurement strategy described here:
http://www.codeproject.com/Articles/61964/Performance-Tests-Precise-Run-Time-Measurements-wi
What I have observed is that there is over a magnitude loss in performance in executing the single line that retrieves the httpRequest from the Properties collection. Without this line of code I see >700 completions within 5 seconds. When I add that line in, it drops to <20 completions within 5 seconds.
Has anyone else experienced this? Is there a different method I should use to cache data for the length of a single request that will be available throughout the Http pipeline?
Adding an item to the Current HttpContext Items collection appears to work and has no discernable impact on the performance I am measuring.