1. Using HttpWorkerRequest for getting headers
1、使用HttpWorkerRequest获取headers信息

First, the HttpWorkerRequest class is used internally by ASP.NET, and provides a lower-level way of accessing ASP.NET internals. In this code, we see that you can use the HttpContext to call GetService and get the current worker. Then, you can use the same code that the intrinsic objects use, but with no overhead.
首先,HttpWorkerRequest类由ASP.NET内部使用,并提供了访问ASP.NET内部的低级方法。在这段代码中,我们看到你可以使用HttpContext来调用GetService并获取当前的worker。然后,您可以使用内部对象使用的相同代码,但没有开销。

示例一(注意:这里是在一般处理程序中定义的):

using System;
using System.Web;
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//
// Get the Referer with HttpWorkerRequest.
//
string referer = worker.GetKnownRequestHeader(HttpWorkerRequest.HeaderReferer);
//
// Get the Accept-Encoding with HttpWorkerReqest
//
string acceptEncoding = worker.GetKnownRequestHeader(HttpWorkerRequest.HeaderAcceptEncoding);
//
// Display the values.
//
HttpResponse response = context.Response;
response.Write("Referer: ");
response.Write(referer != null);
response.Write(" Accept-Encoding: ");
response.Write(acceptEncoding);
} public bool IsReusable
{
get
{
return false;
}
}
}

Description of the HttpHandler.
HttpHandler的描述。

This is a generic handler you can run in the code-behind file of a HTTP handler. You can use the contents of the ProcessRequest method anywhere in ASP.NET, through. The first lines with IServiceProvider simply use an interface to get the HttpWorkerRequest.
这是一个通用的处理程序,可以在HTTP处理程序的代码隐藏文件中运行。您可以在ASP.NET中的任何位置使用ProcessRequest方法里的内容。 IServiceProvider的第一行只是使用一个接口来获取HttpWorkerRequest。

Using the worker instance.
worker实例的使用。

The example shows how you can get the referer of the page and also the Accept-Encoding headers. You can do this with the Request object, but it is slower and more prone to errors. Finally, the example prints the text of the headers it accessed.
该示例显示了如何获取页面的引用者以及Accept-Encoding标头。您可以使用Request对象执行此操作,但速度较慢且更容易出错。最后,该示例将打印它所访问的标题的文本。

2. Using HttpWorkerRequest for setting headers
2、使用HttpWorkerRequest设置请求头信息

Here we see how you can set HTTP headers with the HttpWorkerRequest. This allows you to bypass the ASP.NET AddHeader method, which has a fair amount of overhead. We specify that the handler should be cached for 2 hours here. The SendKnownResponseHeader method is not exactly the same as AddHeader, but sometimes you can use it instead.
在这里,我们看到如何使用HttpWorkerRequest来设置HTTP头。这使您可以绕过有着很大开销的ASP.NET AddHeader方法。我们指定处理程序应该在这里缓存2个小时。 SendKnownResponseHeader方法与AddHeader不完全相同,但有时您可以使用它。

示例二:

public void ProcessRequest(HttpContext context)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//
// Set Cache-Control with HttpWorkerRequest.
//
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderCacheControl, "private, max-age=7200");
}

3. Gotchas with HttpWorkerRequest
3、HttpWorkerRequest的缺陷

The HttpWorkerRequest is not commonly used in simple or small ASP.NET projects, and it is much harder to use. For example, its settings can interact in different ways with your Web.config. Setting the Content-Length is very tricky to get right. Due to the complexity of the class, these are things you will have to hack through.
HttpWorkerRequest并不常用于简单或小型的ASP.NET项目,而且使用起来更加困难。例如,它的设置可以用不同的方式与您的Web.config进行交互。设置Content-Length是非常棘手的。由于class的复杂性,这些都是你必须破解的东西。

4. Performance of HttpWorkerRequest
4、HttpWorkerRequest的性能(使用HttpWorkerRequest与ASP.NET AddHeader方法比较)

Here we see a simple benchmark that compares setting two HTTP headers on a response. The first method uses the Response object, and the second method uses the HttpWorkerRequest object. Internally, the first version will call into the same methods as the second version. In other words, the result is obvious from the internal layout of the runtime.
在这里,我们看到一个简单的基准测试,它比较了在响应中设置两个HTTP头。第一种方法使用Response对象,第二种方法使用HttpWorkerRequest对象。在内部,第一个版本将调用与第二个版本相同的方法。换句话说,从运行时的内部布局来看结果是显而易见的

示例三:
=== HTTP header method versions benchmarked ===
=== 基于HTTP标头方法的版本 ===

public static void Set1(HttpContext context, string contentEncoding, string cacheControl)
{
context.Response.AddHeader("Content-Encoding", contentEncoding);
context.Response.AddHeader("Cache-Control", cacheControl);
} public static void Set2(HttpContext context, string contentEncoding, string cacheControl)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderContentEncoding, contentEncoding);
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderCacheControl, cacheControl);
}

=== Calling code (1 million iterations) ===
=== 调用代码 (100万次迭代) ===

Set1(context, "gzip", "private, max-age=7200");
Response.ClearHeaders(); Set2(context, "gzip", "private, max-age=7200");
Response.ClearHeaders();

=== Benchmark results ===
=== 基准测试结果 ===

Set1 Response:          895 ms
     Note:              Uses AddHeader
Set2 HttpWorkerRequest: 488 ms
     Note:              Uses SendKnownResponseHeader

5. Intrinsic objects in ASP.NET
5、ASP.NET中的内部对象

You can accomplish almost everything that HttpWorkerRequest lets you do with the Context, Request and Response instrinsic objects. However, when you use Request and Response, they execute complicated and slow logic. Eventually, these objects then use HttpWorkerRequest themselves.
您几乎可以完成HttpWorkerRequest允许您使用Context,Request和Response内部对象执行的所有操作。但是,当您使用请求和响应时,它们执行复杂和慢速的逻辑。最终,这些对象自己使用HttpWorkerRequest。

Example of using referer.
使用referer的例子。
When you call the UrlReferer property on Request, the property does several string comparisons and then creates a new Uri object. This causes a heap allocation. If you check the UrlReferer on every request, this overhead can add up.
在Request上调用UrlReferer属性时,该属性将执行多个字符串比较,然后创建一个新的Uri对象。这会导致堆分配。如果你检查每个请求的UrlReferer,这个开销可以加起来

AppendHeader method.
AppendHeader方法。
When you open the AppendHeader method in ASP.NET, you will find a lot of complex logic and error checking. Often you do not need all this overhead. Internally, the method also calls into the HttpWorkerRequest.
当您在ASP.NET中打开AppendHeader方法时,您会发现很多复杂的逻辑和错误检查。通常你不需要这些开销。在内部,该方法也调用HttpWorkerRequest。
(自己备注:这一小节目的是为了说明HttpWorkerRequest性能开销比直接使用ASP.NET内部对象更节省、效率更高。)

6. Resources
6、资源

You have very likely seen the MSDN topic about this class, but it bears repeating. It states that usually "your code will not deal with HttpWorkerRequest directly." However, it adds that this may be necessary to implement if you are implementing your own hosting environment. [HttpWorkerRequest Class - MSDN]
你很可能已经看到了有关这个类的MSDN主题,但它重复。它指出通常“你的代码不会直接处理HttpWorkerRequest”。然而,它补充说,如果你正在实现你自己的宿主环境,这可能是必要的。 [HttpWorkerRequest类 -  MSDN]

An excellent blog resource.
一个优秀的博客资源。

The most helpful resource on using this class directly is by Daniel Cazzulino. He shows how you can use GetKnownRequestHeader for very specific requirements relating to networks. [ASP.NET low-level fun - Daniel Cazzulino's Blog - weblogs.asp.net]
关于直接使用这个类的最有用的资源是Daniel Cazzulino。他展示了如何使用GetKnownRequestHeader来处理与网络相关的特定需求。 [低级趣味的ASP.NET  -  Daniel Cazzulino的博客 -  weblogs.asp.net]

7. Real-world results with HttpWorkerRequest
7、HttpWorkerRequest的真实结果
The author modified his code to use HttpWorkerRequest in 4 places. The result is that each request is processed about 20 microseconds faster. These timings are real-world and use the accurate Stopwatch class on the requests.
作者修改了他的代码,在4个地方使用HttpWorkerRequest。其结果是每个请求处理大约20微秒。这些时间是现实世界,并在请求上使用准确的Stopwatch类。

8. Summary
8、总结
Here we saw how you can use the 'secret' HttpWorkerRequest to develop web applications that are faster and have clearer code in some respects. This is considered a lower-level interface to ASP.NET, and it should be used with care and testing. For scalability and performance, the HttpWorkerRequest is superior. Using it reduces allocations and avoids lots of code execution.
在这里,我们看到了如何使用“秘密的”HttpWorkerRequest来开发更快速的Web应用程序,并且在某些方面具有更清晰的代码。这被认为是ASP.NET的底层接口,应该谨慎使用和测试。对于可伸缩性和性能,HttpWorkerRequest是优越的。使用它可以减少分配,避免大量的代码执行。

本文内容来源:https://www.cnblogs.com/tuyile006/archive/2009/08/09/1542355.html

HttpWorkerRequest应用简介的更多相关文章

  1. ASP.NET Core 1.1 简介

    ASP.NET Core 1.1 于2016年11月16日发布.这个版本包括许多伟大的新功能以及许多错误修复和一般的增强.这个版本包含了多个新的中间件组件.针对Windows的WebListener服 ...

  2. MVVM模式和在WPF中的实现(一)MVVM模式简介

    MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...

  3. Cassandra简介

    在前面的一篇文章<图形数据库Neo4J简介>中,我们介绍了一种非常流行的图形数据库Neo4J的使用方法.而在本文中,我们将对另外一种类型的NoSQL数据库——Cassandra进行简单地介 ...

  4. REST简介

    一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...

  5. Microservice架构模式简介

    在2014年,Sam Newman,Martin Fowler在ThoughtWorks的一位同事,出版了一本新书<Building Microservices>.该书描述了如何按照Mic ...

  6. const,static,extern 简介

    const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...

  7. HTTPS简介

    一.简单总结 1.HTTPS概念总结 HTTPS 就是对HTTP进行了TLS或SSL加密. 应用层的HTTP协议通过传输层的TCP协议来传输,HTTPS 在 HTTP和 TCP中间加了一层TLS/SS ...

  8. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  9. Cesium简介以及离线部署运行

    Cesium简介 cesium是国外一个基于JavaScript编写的使用WebGL的地图引擎,一款开源3DGIS的js库.cesium支持3D,2D,2.5D形式的地图展示,可以自行绘制图形,高亮区 ...

随机推荐

  1. [转]五步git操作搞定Github中fork的项目与原作者同步

    命令如下: git clone xxx-fork.git git remote add xxx xxx.git git fetch xxx git merge xxx/master git push ...

  2. mockjs从入门到精通视频教程

    Mock.js 是一款模拟数据生成器,旨在帮助前端攻城师独立于后端进行开发,帮助编写单元测试.提供了以下模拟功能: (1)根据数据模板生成模拟数据 (2)模拟 Ajax 请求,生成并返回模拟数据 (3 ...

  3. OpenWrt下如何配置网络?

    答: 使用uci进行配置,示例如下: uci get network.wan.ifname (笔者得到eth1) uci set network.wan.ifname=ethx (如: uci set ...

  4. C++ STL——map和multimap

    目录 一 map和multimap 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 map和multimap map相对于set的区别:map具有键值和实值,所有元素根据键值自动排序.pai ...

  5. LC 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  6. 微信小程序:undefined Expecting 'STRING', got INVALID

    出现问题: 问题原因:app.json中不能出现注释

  7. idea使用generatorconfig生成

    在maven工程中的resource中创建generatorConfigxml配置generatorConfigxml的配置pomxml生成对象的两种方式方式一使用idea的maven插件直接快速生成 ...

  8. centos6 安装docker

    docker 安装要求内核大于3.10 , 而centos6 机器上内核一般是2.6 , 除了升级内核外, 还可以安装低版本的docker , 本文介绍docker 1.7的安装. 机器 环境 [ro ...

  9. Spring-Kafka —— 消费如何达到最高的吞吐量

    首先简单的介绍一下消费者对topic的订阅.客户端的消费者订阅了topic后,如果是单个消费者,那么消费者会顺序消费这些topic分区中的数据,如果是创建了消费组有多个消费者,那么kafak的服务端将 ...

  10. 【机器学习】QQ-plot深入理解与实现

    QQ-plot深入理解与实现 26JUN June 26, 2013 最近在看关于CSI(Channel State Information)相关的论文,发现论文中用到了QQ-plot.Sigh!我承 ...