当的WebAPI访问的Htt prequestMessage属性集合性能问题 [英] Performance Issues when accessing HttpRequestMessage Properties collection in WebAPI

查看:128
本文介绍了当的WebAPI访问的Htt prequestMessage属性集合性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC4我已经创建了一个的WebAPI项目DelegatingHandler。我用的是处理程序中插入一些数据到的Htt prequestMessage属性集合,以便以后的HTTP管道的部分(如其他处理程序,控制器)可以访问这些数据。

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.

例如:

var httpRequest = (HttpRequestMessageProperty) request.Properties["httpRequest"];
httpRequest.Headers["MY_DATA"] = "some data";

我想测试这一对的WebAPI请求处理的吞吐量的影响,所以我创建了一个控制台应用程序,在一个Action委托,实例化一个HttpClient的,发送一个请求,并等待发送下一个之前的响应。我算这个动作代表了不到5秒窗口使用绩效衡量战略成功执行这里所描述的数量: 的http://www.$c$cproject.com/Articles/61964/Performance-Tests-$p$pcise-Run-Time-Measurements-wi

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

我的观察是,有超过性能上的损失幅度在执行检索的属性集合HTT prequest单行。如果没有这一行的code我看到在5秒内> 700完井。当我再补充一点线,它丢弃到< 20完井5秒内

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.

任何人都经历呢?是否有不同的方法,我应该使用缓存数据的单个请求的长度将在整个HTTP管道?

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?

推荐答案

添加项目到当前HttpContext的Items集合似乎工作,并具有我衡量对性能没有明显的影响。

Adding an item to the Current HttpContext Items collection appears to work and has no discernable impact on the performance I am measuring.

HttpContext.Current.Items.Add(REQUEST_ID_KEY, request.GetCorrelationId().ToString());

这篇关于当的WebAPI访问的Htt prequestMessage属性集合性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆