HttpWorkerRequest应用简介

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

posted @ 2017-11-10 15:19  skybirdzw  阅读(774)  评论(0编辑  收藏  举报