5

私は IHttpHandler を持っていますが、これはセットアップに費用がかかり、スレッドセーフであるため、再利用の恩恵を受けることができると信じています。ただし、リクエストごとに新しいハンドラーが作成されています。ハンドラーが再利用されていません。

以下は、高価なセットアップなしの私の簡単なテストケースです。この単純なケースは私の問題を示しています:

public class MyRequestHandler : IHttpHandler
{
    int nRequestsProcessed = 0;

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        nRequestsProcessed += 1;
        Debug.WriteLine("Requests processed by this handler: " + nRequestsProcessed);
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
}

Requests processed by this handler: 1
Requests processed by this handler: 1
Requests processed by this handler: 1
Requests processed by this handler: 1... at least 100 times. I never see > 1.

IsReusable の仕組みを誤解していますか? 再利用を打ち負かすことができるものは他にありますか? 私のハンドラは Silverlight アプリケーションから呼び出されています。

4

2 に答える 2

3

IsReusable は保証ではありません。

ハンドラーをリファクタリングして、すべてのクロスリクエスト状態を別のクラスに入れるだけです。Web アプリケーションのクロス リクエスト状態を明確に分離するのがベスト プラクティスです。危険だからです。

于 2012-09-02T21:03:25.700 に答える