通過IStreamWriter和IHasOptions的實現返回圖像/ jpeg結果,如果WriteTo發生錯誤,AppHost中的全局錯誤處理程序不會被調用,並且圖像/ jpeg標題保留,這會導致HTML錯誤(由ServiceStack生成)帶有圖像/ jpeg HTTP標頭。使用ServiceStack的IStreamWriter&IHasOptions處理錯誤
下面是如何重現這樣的一個例子:
public class SampleStreamWriter : IStreamWriter, IHasOptions
{
void WriteTo(Stream responseStream)
{
// This would actually be a delegate
throw new ApplicationException("...");
}
public IDictionary<string, string> Options
{
get
{
return new Dictionary<string, string>
{
{HttpHeaders.ContentType, "image/jpeg"}
};
}
}
}
由於功能的writeTo之前被調用,它是不可能的try/catch內的writeTo並更改內容類型,以例如「application/json」,並手動覆蓋錯誤響應。
這是如何實現的,以便HTTP響應具有錯誤的Content-Type值,並且作爲獎勵,AppHost的ServiceExceptionHandler被調用以進行日誌記錄?
這是有道理的;這意味着在直接流入響應或更好的錯誤處理之間存在折衷。然後,我將回退到返回一個MemoryStream並直接在服務中寫入Response ContentType。謝謝你的幫助! – 2013-05-15 22:25:00