views:

26

answers:

1

Assume an ASP.NET page with these lines of code:

while (true)
{
   byte[BUFFER_SIZE] buffer;
   // Fill buffer with pseudo data
   Response.OutputStream.Write(buffer, 0, buffer.Length);
   Response.Flush();
}

And a test application for above page with these lines of code:

while (readBytes != 0)
{
   byte[BUFFER_SIZE] buffer;
   readBytes = stream.Read(buffer, 0, buffer.length)
}

Instead of reading a real huge file I tried to produce random binary data on the fly and transfer to the client and it worked fine. Then I wrote a client who just read the bytes from ASP.NET page and then do nothing with it and just read the next bytes until the connection is closed by ASP.NET page (download is finished). I used a time-based setting in ASP.NET page for when to finish the download (this is a stress test).

So what IIS 7.0 and ASP.NET are doing is streaming data for a period of time (i.e one hour). Then I started 100 concurrent clients on this page with average download speed of 250 KB/s for one hour. Everything was OK for about 45 minutes but suddenly IIS closes all connections with this error code on the client side: Unable to read data from the transport connection: The connection was closed. I guess it's about transferring a lot of binary data via IIS and there should be some configuration in IIS 7.0 which is triggered by my stress test but which configuration ?

A: 

The answer is funny but lets review why I asked this question and how I found the answer.

After I encountered the problem I searched the web and found out similar questions never had an answer. To my experience when you ask a question that no body else asked or when there are many similar questions without a chance to get answered something is wrong with the question but I didn't think to it at that moment. After getting disconnected I changed the stress test parameters and find out clients are disconnected at the end of the time of test (as I told the test was designed time-based). Surly somewhere in my code I was doing a wrong thing, somewhere in shutting down the request. Despite of everywhere in .NET where you should close connections, free resources and calling dispose on objects here in ASP.NET you should not call this method at all :

Response.Close()

After you are done in an ASP.NET page you should just let the IIS do the rest (let the execution path to leave your code). I believe something is wrong about it. Maybe it should not be public in an asp.net page scope or maybe the method name should be something like Response.Disconnect() because it's very similar to clean up code.

Yes I was kicking myself by disconnecting clients with Response.Close()

After removing that code everything is OK but when the machine free RAM is not enough I get this error which is welcome because you can overcome this by adding some RAM :

An existing connection was forcibly closed by the remote host.
Xaqron