We've got a fairly complex httphandler for handling images. Basically it streams any part of the image at any size that is requested. Some clients use this handler without any problems. But we've got one location that gives us problems, and now it also gives problems on my development environment.
What happens is that the client never receives anything on some requests. So request 1 and 2 are fine, but request 3 and 4 never end.
- While debugging I can see that the server is ready and has completed the request.
- The client however is still waiting on a result (debugging with fiddler2 shows that there is no response received)
The code that we use to stream an image is
if (!context.Response.IsClientConnected)
{
imageStream.Close();
imageStream.Dispose();
return;
}
context.Response.BufferOutput = true;
context.Response.ContentType = "image/" + imageformat;
context.Response.AppendHeader("Content-Length", imageStream.Length.ToString());
if (imageStream != null && imageStream.Length > 0 && context.Response.IsClientConnected)
context.Response.BinaryWrite(imageStream.ToArray());
if (context.Response.IsClientConnected)
context.Response.Flush();
imageStream.Close();
imageStream.Dispose();
The imageStream is a MemoryStream with the contents of an image.
After the call to response.Flush() we do some more clean-up and writing summaries to the eventlog.
We also call GC.Collect() after every request, because the objects that we use in-memory become very large. I know that that is not a good practice, but could it give us trouble?
The problems with not returning requests happen at both IIS 5 (Win XP) and IIS 6 (Win 2003), we use .NET framework v2.