Saturday, July 30, 2011

IIS 7, App pool Bug

Recently I encountered a strange issue in our application. We use WCF Restful services which is consumed by a client application. Application is to work when we have a frequent service calls. But, after keeping the application idle for sometime, services stopped responding for all the requests. Strange!!!
The response from the server confused us more. Once the services stops responding we use to get Error Code 401.5 - Which is related to authentication.
We spent lot of time in understanding the root cause of this issue.

Luckily, WCF tracer gave us the hint. The actual issue was not in WCF Restful services rather it was in IIS 7, application pool. Let me explain how it works.

Host a WCF Restful service on IIS 7 by creating new App. Pool. By default App. pool Process Idle timeout is 20 minutes. If you keep the system Idle for more than 20 minutes, App. Pool will be unloaded automatically by IIS 7.

As per the MS document, Whenever there is a new service call, if App. pool is unloaded, IIS 7 will automatically reloads App. Pool which will stay active for next 20 minutes.

This works perfectly when the client application hits the service end point.
For eg: http://myserver/myservices/service.svc

But if you have hosted WCF Restful service, client application hits the Restful end-point instead of service end point
For eg: http://myserver/myservices/service.svc/GetEmployees

Here is where we have bug in IIS 7. If the client application hits the Restful end point, IIS 7 fails to reload the App. pool and the request fails to reach the services.

Workarounds:
Solution 1: Client applications should make a dummy Http Web Request to the services before making any PUT operation (may be even for GET operations). By doing this IIS re-creates the App Pool and services starts working.

Solution 2: There should be sub-system which make a frequent call to the services just to make sure that application pool is always alive.
http://stackoverflow.com/questions/838318/how-to-keep-asp-net-assemblies-in-appdomain-alive

Solution 3: Setting the App. Pool Idle timeout to “Zero”. This is not recommended solutions.
Suggested app. Pool command:
c:\windows\system32\inetsrv\appcmd.exe add apppool /name:"[APP_POOL_NAME]" /managedPipelineMode:Classic /managedRuntimeVersion:v2.0 /enable32BitAppOnWin64:true /processModel.idleTimeout:00:00:00
Doing this doesn’t make any harm to the system. IIS 7 will recycle the system once it reaches it memory limit.

No comments:

Post a Comment