<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>HttpHandlers and HttpModules</title><link>http://forums.asp.net/27.aspx</link><description>Extending the ASP.NET Framework through HttpModules and HttpHandlers.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: How do you ensure that AppDomain event handlers in HttpModule stay alive?</title><link>http://forums.asp.net/thread/1999715.aspx</link><pubDate>Sat, 10 Nov 2007 15:20:24 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1999715</guid><dc:creator>bitmask</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1999715.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=27&amp;PostID=1999715</wfw:commentRss><description>&lt;p&gt;I don&amp;#39;t think there is any guarantee that a specific HttpModule instance will remain in the processing pipeline for the life of the application. The object could get disposed - which could give you trouble. &lt;/p&gt;
&lt;p&gt;Remember, though, that disposed isn&amp;#39;t the same as removing the object from memory. Since&amp;nbsp;you are storing a delegate with a reference to your object in a global area ( AppDomain.CurrentDomain.UnhandledException), the garbage collector won&amp;#39;t be able to take your object away. I&amp;#39;d probably create a new, non-HttpModule derived class with the single responsibility of listening for unhandled exceptions, so I woulnd&amp;#39;t have to worry about being disposed. &lt;/p&gt;</description></item><item><title>How do you ensure that AppDomain event handlers in HttpModule stay alive?</title><link>http://forums.asp.net/thread/1996494.aspx</link><pubDate>Thu, 08 Nov 2007 14:47:52 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1996494</guid><dc:creator>foreachbiscuit</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1996494.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=27&amp;PostID=1996494</wfw:commentRss><description>&lt;p&gt;Hi,&lt;br /&gt;I&amp;#39;ve got a HttpModule that providers an event handler to the AppDomain event UnhandledException.&lt;br /&gt;This is specifically to catch any worker thread exceptions that occur outside the context of an asp.net request.&lt;br /&gt;(In 2.0 such errors kill the application immediately with event log error).&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Here is a snippet of the module with Init code:&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;static int&lt;/span&gt; unhandledExceptionCount = 0;&lt;br /&gt;&lt;span class="kwd"&gt;static object&lt;/span&gt; lockObject = &lt;span class="kwd"&gt;new object&lt;/span&gt;();&lt;br /&gt;&lt;span class="kwd"&gt;static bool&lt;/span&gt; initialized = &lt;span class="kwd"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwd"&gt;public void&lt;/span&gt; Init(HttpApplication context)&lt;br /&gt;{&lt;br /&gt;	&lt;span class="cmt"&gt;// We only want one single instance of a HttpModule to subscribe to the unhandled event.&lt;br /&gt;	// This is because the event is not called inside a HttpApplication (and thus one corresponding Module)&lt;/span&gt;&lt;br /&gt;	&lt;br /&gt;	&lt;span class="kwd"&gt;if&lt;/span&gt;( !initialized)&lt;br /&gt;	{&lt;br /&gt;		&lt;span class="cmt"&gt;// create a lock so no other thread/HttpModule instance can affect the static variable&lt;/span&gt;&lt;br /&gt;		&lt;span class="kwd"&gt;lock&lt;/span&gt; (lockObject)&lt;br /&gt;		{&lt;br /&gt;			&lt;span class="kwd"&gt;if&lt;/span&gt;( !initialized)&lt;br /&gt;			{&lt;br /&gt;				AppDomain.CurrentDomain.UnhandledException +=&lt;span class="kwd"&gt;new&lt;/span&gt; UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);&lt;br /&gt;				initialized = &lt;span class="kwd"&gt;true&lt;/span&gt;;&lt;br /&gt;			}&lt;br /&gt;		&lt;span class="cmt"&gt;// now lock is released and the static variable is true.. we have attached the event handler to one instance&lt;/span&gt;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&lt;p&gt;Because there are multiple HttpApplication objects pooled within an AppDomain, there will be multiple HttpModules - one for each HttpApplication object.&lt;br /&gt;So the above code is ensuring that only one of the modules can register the eventhandler and is therefore thread/multiple instance aware..&lt;/p&gt;&lt;p&gt;Now this works great and has been unit tested with some success.&lt;br /&gt;&lt;br /&gt;However, my question is... if some of the HttpApplication pool objects are disposed of when the runtime deems them unnecessary (e.g the site was busy for some time, and is now quiet), how can we know if the HttpModule that has the handler is still alive?&lt;/p&gt;&lt;p&gt;If the HttpApplication containing the insance of the HttpModule that first hooked up the event handler is killed off, there will be nothing to catch the AppDomain.UnhandledException...&lt;/p&gt;&lt;p&gt;Does anyone have any ideas on this?&lt;/p&gt;&lt;p&gt;Should we use the Dispose method of the HttpModule instance that has the eventhandler and release the &amp;quot;initialized&amp;quot; static bool so another can register when it starts up?&lt;/p&gt;&lt;p&gt;Or is something that we don&amp;#39;t have to worry about? Will the original HttpApplication/HttpModule be kept alive?&lt;br /&gt;&lt;/p&gt;</description></item></channel></rss>