Saturday, 6 July 2013

Solution for Thread being Aborted, Call a long running Webservice using SoapRequest from asp.net page and Usage of Health Monitoring system

Sometimes we had to face a problem thread being aborted while long running web service  which is call from a website through soap request.

Why this error is raising while doing a long running process in ASP.Net 2.0 , Answer from the Microsoft is IIS is not designed for a long running process.

 What makes the running thread to be Aborted in ASP.Net ?
  • while any error is raised in your application, if there is no catch block, because of  UnHandledException Thread is begin Aborted abruptly.
  • RoundRobin Request Makes AppDomain to ReCycle.
  • After a IdleTime Out ApplicationPool Automatically Recycles (If there is no more request is received for asp.net page from client more than IdleTimeOut minutes)
   If Recycle  is done by Application Pool, then following are the Reasons 

IIS may have recycled the application pool for whatever reason.

IIS may have restarted

Some settings on the server that limits asp.net execution time.


  To Avoid the thread Being Aborted , if it process by Application pool, Increase the Execution Time Out of Web  Service

Change the web.config file and setting in IIS.

Increase the executionTimeOut and application pool ideal time out.


Timeout in Debug mode
          Default script timeout on IIS is only 90 seconds. When you start web application in debug mode it is 300 000 seconds (almost a year). So, timeout errors are visible only when running release code.

Change server timeout in Web.config

              If you want to have an option to change server timeout value later, after code is compiled, one of easy ways is to change it as parameter through a web.config. You can set server timeout with code like this:    Default executionTimeout is 90 seconds.

<configuration>
   <system.web>
      <httpRuntime maxRequestLength="4000" executionTimeout="45" />
   </system.web>
</configuration>


Change Application Pool Ideal Timeout
  1. Open IIS Manager.type inetmgr in run
  2. In the Connections pane, expand the server node and click Application Pools.
  3. On the Application Pools page, select the application pool for which you want to specify idle time-out settings, and then click Advanced Settings in the Actionspane.
  4. In the Idle Time-out (minutes) box, type a number of minutes, and then click OK.






Problem with application restart when running long tasks in ASP.NET

when work with long running tasks. Application could restart because of different outside reasons, and then your long operation is stopped too. The reason can be
  • Change in global.asax file
  • Change in web.config file
  • Change in machine.config file
  • Change in content of bin folder 
  • Add a new folder inside App folder
  • Even some antivirus also reason

    Health Monitoring 
          How we can find out which makes the Thread being Aborted for ASP.Net ?

Health Monitoring : Health Monitoring is the One of the Framework that is forgotten by Asp.Net Developers. It Provides great Monitoring features in Diagnose Failing Application and systems. Asp.Net Errors are placed in system event logs.

To Enable that Change in Master Web.Config file which is placed in Dotnet framework installation directory %WINDIR%\Microsoft.NET\Framework\version\CONFIG . From the Config file we removed some 
markup to show clearly.

 <configuration>
  <system.web>
  <healthMonitoring>
  <eventMappings>
    <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647"/>
    <add name="Failure Audits" type="System.Web.Management.WebFailureAuditEvent,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647"/>
  </eventMappings>

  <providers>
    <add name="EventLogProvider" type="System.Web.Management.EventLogWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
    <add connectionStringName="LocalSqlServer" maxEventDetailsLength="1073741823" buffer="false" bufferMode="Notification" name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
  </providers>

  <rules>
  <add name="All Errors Default" eventName="All Errors" provider="EventLogProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom=""/>
 <add name="Failure Audits Default" eventName="Failure Audits" provider="EventLogProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom=""/>
  </rules>
  </healthMonitoring>
  </system.web>
</configuration>
 

Health Monitoring :
1. Monitor the health and performance of the application.
2. Diagnose the application or system failures.
3. appraise significant events during the life cycle of an application.
4. Monitor live ASP.NET applications, individually or across a Web farm.
5. Log events that do not necessarily relate to errors in an ASP.NET application.

the <eventMappings> element assigns the human-friendly name "All Errors" to  the health monitoring events of type WebBaseErrorEvent and the name "Failure Audits" to health monitoring events of type WebFailureAuditEvent

The <providers> element defines the log sources,The first <add> element defines the "EventLogProvider" provider, which logs the specified health monitoring events using the EventLogWebEventProvider class. The EventLogWebEventProvider class logs the event to the Windows Event Log. The second <add> element defines the "SqlWebEventProvider" provider, which logs events to a Microsoft SQL Server database via the SqlWebEventProvider class. The "SqlWebEventProvider" configuration specifies the database's connection string (connectionStringName) among other configuration options.

The <rules> element maps the events specified in the <eventMappings> element to log sources in the<providers> element. By default, ASP.NET web applications log all unhandled exceptions and audit failures to the Windows Event Log.


Now change the Web.Config file in your Asp.Net Application
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>

    <healthMonitoring enabled="true" heartbeatInterval="120">
      <rules>
        <add name="Heartbeats Default" eventName="Heartbeats" provider="EventLogProvider"
              profile="Default" />
        <add name="Application Lifetime Events Default" 
              eventName="Application Lifetime Events" 
              provider="EventLogProvider"
              profile="Default" />
        <add name="Request Processing Events Default" 
              eventName="Request Processing Events" 
              provider="EventLogProvider"
              profile="Default" />
      </rules>
    </healthMonitoring>
  </system.web>
</configuration>
After deploy the application , when we launch the application we can see the event log with codes 1001 , 1002 gives information about application start up and shut down. code 1005 traced every 120 seconds (hearbeatinterval = 120).
From this log we can say whether our application is consuming to much memory or queuedin user request.
From This article we can find the various possibilities of Thread Aborting for a long running task,
And Process of Health Monitoring

4 comments:

  1. This blog fixed my issue. Thanks

    ReplyDelete
    Replies
    1. Which step resolved the issue in your case?

      Delete
    2. You can try to increase the httpRuntime Execution Timeout, as well as IdleTimeout in App pool and increase timeout in web.config, may resolve issue in timeout

      Delete