“Invalid use of response filter” when compressing response from an IHttpHandler

不问归期 提交于 2019-12-12 19:27:47

问题


I have an IHttpHandler returning a file. When the response stream is compressed, either automatically using Telerik RadCompression or by explicitly setting a filter using

context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

the response returned to the browser is correct but at the end of the response is some HTML. The HTML contains the exception:

[HttpException (0x80004005): Invalid use of response filter]
   System.Web.HttpResponseStreamFilterSink.VerifyState() +3928894
   System.Web.HttpResponseStreamFilterSink.Write(Byte[] buffer, Int32 offset, Int32 count) +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +363
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +595
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +754
   System.Web.HttpResponse.FilterOutput() +121
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +174

If I make sure the response is NOT compressed, the response contains no exception.

What's up with that?


回答1:


It seems to be related to this: GZipStream is cutting off last part of XML

I removed the compression module and it seem to be all good now.




回答2:


Make sure you're not preventing buffering. Apparently, to compress the response, you have to allow it to be buffered.

I.e., one of the following two lines needs to be removed:

context.Response.BufferOutput = false;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);


来源:https://stackoverflow.com/questions/4797829/invalid-use-of-response-filter-when-compressing-response-from-an-ihttphandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!