其實絕大多數一般的網站都可以直接搬上Azure Website,不過最近剛好有客戶需要,因此還是寫起來。
- 首先我們需要一個HttpModule以及一個Custom Response Filter
- 建立一個HttpModule,在Init()中加入以下程式碼
context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
HttpContext httpContext = ((HttpApplication)sender).Context;
HttpResponse response = httpContext.Response;
response.Filter = new IWantMyResponseLengthStream(response.Filter);
}
接著我們需要撰寫一個IWantMyResponseLengthStream類別,繼承自MemoryStream
public class IWantMyResponseLengthStream: MemoryStream
{
private readonly Stream responseStream;
private long responseLen = 0;
public override long Length
{
get
{
return responseLen ;
}
}
public IWantMyResponseLengthStream(Stream responseStream)
{
this.responseStream = responseStream;
}
public override void Write(byte[] buffer, int offset, int count)
{
this.responseLen += count;
this.responseStream.Write(buffer, offset, count);
}
public override void Flush()
{
//this.Length就是回應的訊息大小了
//var requestURL = HttpContext.Current.Request.Url.ToString();
//var remoteIP = HttpContext.Current.Request.UserHostAddress;
base.Flush();
}
}
接著確認我們的Azure Website服務的pipeline模式是Classic還是Integrated;在WebSite->Configure->Managed Pipeline Mode
根據不同的Pipeline Mode,修改web.config
然後佈署到Azure WebSite上就可以了。