[bug 逃走中] 在 IIS 6.0 中使用 Response.Headers.Add() 會擲出 "This operation requires IIS integrated pipeline mode" 例外

這是今天發生的一起靈異現象,原本寫好的 HTTP Handler 在 IIS 6.0 和 IIS 7.0 都相安無事,但是今天部署到 IIS 6.0 時,卻發生了 "This operation requires IIS integrated pipeline mode" 的錯誤 ...

這是今天發生的一起靈異現象,原本寫好的 HTTP Handler 在 IIS 6.0 和 IIS 7.0 都相安無事,但是今天部署到 IIS 6.0 時,卻發生了 "This operation requires IIS integrated pipeline mode" 的錯誤,但查遍了 Web.config 的設定,也沒有什麼問題。

後來想到之前有在 Handler 中做了一個寫入 HTTP Header 的動作,當時我用的是 context.Response.Headers.Add(),後來改用了 context.Response.AddHeader() 後,問題就消失了。

我很納悶這個問題的原因,用了 Reflector 去 trace 了一下 System.Web.dll 中的程式碼,在 HttpResponse 類別找到了這段:


public NameValueCollection Headers
{
    get
    {
        if (!(this._wr is IIS7WorkerRequest))
        {
            throw new PlatformNotSupportedException(SR.GetString("Requires_Iis_Integrated_Mode"));
        }
        if (this._headers == null)
        {
            this._headers = new HttpHeaderCollection(this._wr, this, 0x10);
        }
        return this._headers;
    }
}
 
喵的咧 ... 會檢查目前的 HttpWorkerRequest 是不是 IIS 7 的,如果不是,就會擲出這個例外,
但在 MSDN Library 中卻也沒看到這方面的說明,所以 ...

 

算了,bug 在短時間捕獲,收工~