Wednesday, 13 August 2008

IIS Keep - Alive

Lately I have been mulling about with the an error that some of the clients were getting from the webservice hosted on the servers in my organisation. Yes! the much talked about ...

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

TBH, I had never seen this error before this juncture and could hardly figure out where the link was broken. I struggled for few days, struggling to find out a solution to it and then, like my many fellow developers came across the Keep-alive functionality inside IIS . Again it was a conundrum to me, as I had never played with this aspect of IIS much and being a live box, I had to be double sure what I am doing. It boiled down to understanding this Keep- Alive thingy and I thought I should share it within the community. A lot of articles talk about jow it turn it off and on but it was hard for me to find that why turn it off or on and why does IIS need it .

The answer is to be found in the life cycle of asp.net application or for that matter any web application . What happens when a page request comes to IIS ?!? . I think most of the folks know about all this stuff but I don't want to talk about the bigger picture here.

Its all inside the ISAPI extenstions that we get the answer to this .....

The following events occur when IIS receives a request that maps to an ISAPI extension:


  • IIS loads the DLL, if it is not already in memory. When the DLL is loaded, Windows automatically calls the optional DLL entry/exit function (usually DllMain). IIS then calls the extension's GetExtensionVersion entry-point function.

  • IIS performs minor preprocessing on the incoming request.

  • IIS creates and populates an EXTENSION_CONTROL_BLOCK structure to pass request data and callback function pointers to the extension.

  • IIS calls the ISAPI extension's HttpExtensionProc function, passing a pointer to the EXTENSION_CONTROL_BLOCK structure created for this request.

  • The ISAPI extension carries out the actions it was designed to perform: for example, reading more data from the client (as in a POST operation), or writing headers and data back to the client.

  • The extension informs IIS that it is finished processing the request by exiting the HttpExtensionProc function. For synchronous operations, the function returns the HSE_STATUS_SUCCESS return code; for asynchronous operations, the return code is HSE_STATUS_PENDING. For more information about asynchronous operations, see Asynchronous I/O Processing.

  • IIS performs cleanup on the connection used for the request, after which it closes the connection if Keep-Alive functionality is not enabled.

And here is when the IIS Keep-Alive settings are effectively going to determine whether the connection needs to be closed.


  • Once the ISAPI extension is no longer needed, IIS calls the TerminateExtension function, if the extension provides one. If IIS is configured to cache ISAPI extensions, TerminateExtension is not called until the IIS Web server is shut down or restarted.

No comments: