<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Martin Normark&#039;s blog &#187; IIS7</title>
	<atom:link href="http://martinnormark.com/category/iis7/feed" rel="self" type="application/rss+xml" />
	<link>http://martinnormark.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 21:06:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>IISAPP equivalent in IIS 7 and beyond</title>
		<link>http://martinnormark.com/iisapp-equivalent-in-iis-7-and-beyond?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/iisapp-equivalent-in-iis-7-and-beyond#comments</comments>
		<pubDate>Mon, 08 Mar 2010 23:53:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/IISAPP-equivalent-in-IIS-7-and-beyond.aspx</guid>
		<description><![CDATA[If there’s one change I hate about IIS 7 and beyond, it is the lack of the very useful IISAPP script that was present on IIS 6 installations. Run it from a command prompt, and you will see a list &#8230; <p><a class="btn small" href="http://martinnormark.com/iisapp-equivalent-in-iis-7-and-beyond">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>If there’s one change I hate about IIS 7 and beyond, it is the lack of the very useful IISAPP script that was present on IIS 6 installations.</p>
<p>Run it from a command prompt, and you will see a list of all running worker processes, the process id and the application pool name the process is serving. Very useful if you need to shut down a single site.</p>
<p>As I mentioned, that script was removed in IIS 7 and beyond. I find myself constantly running IISAPP without any luck several times a week, and I’ve decided that this is a habit that I cannot leave behind.</p>
<p>So, to solve the problem I’ve created a small .bat file called <strong>iisapp.bat</strong> and copied it to the C:\Windows\System32 folder. The content of the iisapp.bat file is just a single line, that does exactly the same as the old IISAPP did – just using the new APPCMD in IIS 7 instead:</p>
<blockquote>
<pre class="csharpcode">%SystemRoot%\System32\inetsrv\<strong>appcmd list wp</strong></pre>
</blockquote>
<p>Now I can call IISAPP and see a list of running applications:</p>
<p><a href="http://martinnormark.com/wp-content/uploads/2010/03/iisapp-list-iis7.jpg"><img class="alignnone size-full wp-image-271" title="IIS App List IIS 7" src="http://martinnormark.com/wp-content/uploads/2010/03/iisapp-list-iis7.jpg" alt="IIS Application list in IIS 7" width="590" height="298" /></a></p>
<p>And now my day got a whole lot better. Since the BAT file was copied to the System32 folder, there's no need to add anything to the PATH environment variable. This will work from any command prompt - just type <strong>iisapp</strong> and hit Enter.</p>
<div style="float: right;">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS1=1&#038;nou=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=martnormsblog-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;ref=tf_til&#038;asins=0470152532" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<h3>The Integrated Programming model in IIS 7</h3>
<p>IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.</p>
<p>To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There's no limit to what you can do.</p>
<p>I found a lot of great stuff in the book called <a href="http://www.amazon.com/gp/product/0470152532/ref=as_li_tf_tl?ie=UTF8&#038;tag=martnormsblog-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399381&#038;creativeASIN=0470152532">Professional IIS 7 and ASP.NET Integrated Programming</a><img src="http://www.assoc-amazon.com/e/ir?t=martnormsblog-20&#038;l=as2&#038;o=1&#038;a=0470152532&#038;camp=217145&#038;creative=399381" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and learned a lot of useful stuff.</p>
<p>IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/iisapp-equivalent-in-iis-7-and-beyond/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Multiple SSL certificates on IIS using host headers</title>
		<link>http://martinnormark.com/multiple-ssl-certificates-on-iis-using-host-headers?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/multiple-ssl-certificates-on-iis-using-host-headers#comments</comments>
		<pubDate>Thu, 04 Mar 2010 00:35:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/Multiple-SSL-certificates-on-IIS-using-host-headers.aspx</guid>
		<description><![CDATA[In IIS SSL sites have seemed to be limited to only one site per network interface, since you (from IIS Manager) cannot specify a host header binding on the HTTPS protocol. It turns out, that it is only a limitation &#8230; <p><a class="btn small" href="http://martinnormark.com/multiple-ssl-certificates-on-iis-using-host-headers">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>In IIS SSL sites have seemed to be limited to only one site per network interface, since you (from IIS Manager) cannot specify a host header binding on the HTTPS protocol.</p>
<p>It turns out, that it is only a limitation in the UI. So to have e.g. two sites with their own dedicated SSL certificate we need to add a host header binding on port 443 from either appcmd, managed code or by editing the applicationHosts.config file.</p>
<p>I like managed code the most, so I’ve written a small method in C# that does the trick. You need to have two SSL certificates named <a href="http://www.ssl1.com">www.ssl1.com</a> and <a href="http://www.ssl2.com">www.ssl2.com</a> installed on the machine. I just created a self signed certificate for both of them using the IIS Manager.</p>
<pre class="csharpcode"><span class="kwrd">using</span> System.Security.Cryptography.X509Certificates;
<span class="kwrd">using</span> Microsoft.Web.Administration;

<span class="kwrd">namespace</span> IisSsl
{
    <span class="kwrd">class</span> Program
    {
        <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
        {
            <span class="kwrd">using</span> (ServerManager _serverManager = <span class="kwrd">new</span> ServerManager())
            {
                <span class="kwrd">string</span> siteName = <span class="str">"SSL2"</span>;
                <span class="kwrd">string</span> certName = <span class="str">"www.ssl2.com"</span>;

                X509Store store = <span class="kwrd">new</span> X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);

                X509Certificate2 certificate = store.Certificates[0];

                Site site = _serverManager.Sites[siteName];

                <span class="kwrd">if</span> (site != <span class="kwrd">null</span>)
                {
                    site.Bindings.Add(<span class="str">"*:443:"</span> + certName,
                        certificate.GetCertHash(), store.Name);
                }
                store.Close();

                _serverManager.CommitChanges();
            }
        }
    }
}</pre>
<p>Remember to add a reference to C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll in order to use the ServerManager class.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/multiple-ssl-certificates-on-iis-using-host-headers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Migrate web.config to support the IIS 7 Integrated Pipeline</title>
		<link>http://martinnormark.com/migrate-web-config-to-support-the-iis-7-integrated-pipeline?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/migrate-web-config-to-support-the-iis-7-integrated-pipeline#comments</comments>
		<pubDate>Sun, 15 Mar 2009 22:13:47 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/Migrate-webconfig-to-support-the-IIS-7-Integrated-Pipeline.aspx</guid>
		<description><![CDATA[Whenever you deploy a website to IIS 7 that is not compliant with the IIS 7 integrated pipeline, you will get an error like this one: Not the great error message you get. It actually gives you the solution right &#8230; <p><a class="btn small" href="http://martinnormark.com/migrate-web-config-to-support-the-iis-7-integrated-pipeline">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Whenever you deploy a website to <strong>IIS 7</strong> that is not compliant with the IIS 7 integrated pipeline, you will get an error like this one:</p>
<p><a href="http://www.martinnormark.com/image.axd?picture=iis7integratedpipelineerror.png"><img style="display: inline; border: 0px;" title="iis7-integrated-pipeline-error" src="http://www.martinnormark.com/image.axd?picture=iis7integratedpipelineerror_thumb.png" border="0" alt="iis7-integrated-pipeline-error" width="644" height="458" /></a></p>
<p>Not the great error message you get. It actually gives you the solution right away: <strong>Migrate Web.config to support the integrated pipeline. </strong>To do that, start a command prompt, and execute:</p>
<p><strong>%SystemRoot%\system32\inetsrv\appcmd migrate config "test/"</strong></p>
<p>After doing this, our Web.config is changed to support the IIS 7 integrated pipeline and we can see the website.</p>
<div style="float: right;">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS1=1&#038;nou=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=martnormsblog-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;ref=tf_til&#038;asins=0470152532" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<h3>The Integrated Programming model in IIS 7</h3>
<p>IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.</p>
<p>To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There's no limit to what you can do.</p>
<p>I found a lot of great stuff in the book called <a href="http://www.amazon.com/gp/product/0470152532/ref=as_li_tf_tl?ie=UTF8&#038;tag=martnormsblog-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399381&#038;creativeASIN=0470152532">Professional IIS 7 and ASP.NET Integrated Programming</a><img src="http://www.assoc-amazon.com/e/ir?t=martnormsblog-20&#038;l=as2&#038;o=1&#038;a=0470152532&#038;camp=217145&#038;creative=399381" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and learned a lot of useful stuff.</p>
<p>IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/migrate-web-config-to-support-the-iis-7-integrated-pipeline/feed</wfw:commentRss>
		<slash:comments>53</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Adding a Website to IIS7 programmatically</title>
		<link>http://martinnormark.com/adding-a-website-to-iis7-programmatically?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/adding-a-website-to-iis7-programmatically#comments</comments>
		<pubDate>Wed, 16 Apr 2008 19:32:06 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/Adding-a-Website-to-IIS7-programmatically.aspx</guid>
		<description><![CDATA[Some time ago, I blogged about Adding an Application Pool to IIS7 programmatically. The result was a new Application Pool, that uses the Integrated Pipeline in IIS7. In this post I will show you how to add a new Website, &#8230; <p><a class="btn small" href="http://martinnormark.com/adding-a-website-to-iis7-programmatically">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I blogged about <a href="http://www.martinnormark.com/post/Adding-an-Application-Pool-to-IIS7-programmatically.aspx">Adding an Application Pool to IIS7 programmatically</a>. The result was a new Application Pool, that uses the Integrated Pipeline in IIS7.</p>
<p>In this post I will show you how to add a new Website, that uses the Application Pool from the other blog post.</p>
<p>I've loaded the Console Application I used to add the Application Pool, and moved the logic from Main to a method called AddApplicationPool, to split it up nicely. It is actually even easier to add a website programmatically. Below is the code for doing just that:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; height: 283px; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> AddWebSite()
{
  ServerManager mgr = <span style="color: #0000ff;">new</span> ServerManager();

  <span style="color: #0000ff;">if</span> (!Directory.Exists(<span style="color: #006080;">@"c:\inetpub\wwwroot\iis7test"</span>))
  {
    Directory.CreateDirectory(<span style="color: #006080;">@"c:\inetpub\wwwroot\iis7test"</span>);
  }

  <span style="color: #008000;">// Add a new Site to the server, configured to use our the iis7test home directory.</span>
  Site site = mgr.Sites.Add(<span style="color: #006080;">"MyWebSite"</span>, <span style="color: #006080;">@"c:\inetpub\wwwroot\iis7test"</span>, 80);

  <span style="color: #008000;">// Set the application pool name of the site, to use the MyAppPool application pool.</span>
  site.ApplicationDefaults.ApplicationPoolName = <span style="color: #006080;">"MyAppPool"</span>;

  <span style="color: #008000;">// Clear all bindings.</span>
  site.Bindings.Clear();

  <span style="color: #008000;">// Make the site listen to incoming HTTP requests using host header iis7test, on port 80.</span>
  site.Bindings.Add(<span style="color: #006080;">"*:80:iis7test"</span>, <span style="color: #006080;">"http"</span>);

  <span style="color: #008000;">// Set auto start to true.</span>
  site.ServerAutoStart = <span style="color: #0000ff;">true</span>;

  <span style="color: #008000;">// Commit the changes</span>
  mgr.CommitChanges();
}</pre>
</div>
<p>Notice how we add Bindings to the website. Bindings is the information that tells IIS7 when to serve our website. We use this string to configure bindings: *:80:iis7test. The first * tells IIS to listen on all IP addresses on your system. 80 is the port number, and iis7test is the host header value for this site.</p>
<p>To browse our website, we need to add iis7test to the computers hosts file (located in %WINDIR%\System32\Drivers\etc), and point it to 127.0.0.1.</p>
<div style="float: right;">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS1=1&#038;nou=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=martnormsblog-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;ref=tf_til&#038;asins=0470152532" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<h3>The Integrated Programming model in IIS 7</h3>
<p>IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.</p>
<p>To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There's no limit to what you can do.</p>
<p>I found a lot of great stuff in the book called <a href="http://www.amazon.com/gp/product/0470152532/ref=as_li_tf_tl?ie=UTF8&#038;tag=martnormsblog-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399381&#038;creativeASIN=0470152532">Professional IIS 7 and ASP.NET Integrated Programming</a><img src="http://www.assoc-amazon.com/e/ir?t=martnormsblog-20&#038;l=as2&#038;o=1&#038;a=0470152532&#038;camp=217145&#038;creative=399381" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and learned a lot of useful stuff.</p>
<p>IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/adding-a-website-to-iis7-programmatically/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Adding an Application Pool to IIS7 programmatically</title>
		<link>http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically#comments</comments>
		<pubDate>Wed, 16 Apr 2008 19:21:27 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/Adding-an-Application-Pool-to-IIS7-programmatically.aspx</guid>
		<description><![CDATA[Internet Information Services 7 (IIS7) has a great new set of features regarding configuration. Using the Integrated Configuration system, you can configure your server from XML files, the IIS7 manager, the command prompt using the APPCMD tool, but IIS7 also &#8230; <p><a class="btn small" href="http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.iis.net/default.aspx?tabid=7">Internet Information Services 7 (IIS7)</a> has a great new set of features regarding configuration. Using the Integrated Configuration system, you can configure your server from XML files, the IIS7 manager, the command prompt using the APPCMD tool, but IIS7 also lets you manage your server from managed code in a very intuitive manner.</p>
<p>If you navigate to the %WINDIR%\System32\InetSrv folder in Windows Vista, you'll find all the executables, DLL's and XML (.config) configuration files you need. It doesn't matter if you use the IIS7 Manager, managed code or the APPCMD command-line based tool to manage your server - at the end of the day you are changing an XML file. That is <a href="http://www.iis.net/articles/view.aspx/IIS7/Use-IIS7-Administration-Tools/Using-XML-Configuration/Introduction-to-ApplicationHost-config">applicationHost.config</a> which is located here: C:\Windows\System32\inetsrv\config\applicationHost.config.</p>
<p>The integrated configuration system on IIS7 is great news for hosters, and web developers. Hosters can easily automate server management through managed code, and as a web developer, you can configure your server from your web.config file, which makes it easy to move your web application from development, to test, and further up towards production. Read my post on <a href="http://www.martinnormark.com/post/Configuring-IIS-7-default-document-from-webconfig.aspx">how to set a websites default document, from within the web application's web.config file</a>.</p>
<p>In this post, I will create an application pool on my local IIS7.</p>
<p>Launch Visual Studio 2005 or 2008 - whatever you've got will work.</p>
<p>Create a new Console Application, and give it a name of your own choice.</p>
<p>Right click References in the Solution Explorer and add a new reference.</p>
<p><a href="http://www.martinnormark.com/image.axd?picture=WindowsLiveWriter/AddingaVirtualDirectorytoIIS7programmati_124F2/image_2.png"><img style="border-width: 0px;" src="http://www.martinnormark.com/image.axd?picture=WindowsLiveWriter/AddingaVirtualDirectorytoIIS7programmati_124F2/image_thumb.png" alt="image" width="267" height="290" border="0" /></a></p>
<p>Locate Microsoft.Web.Administration.dll from the C:\Windows\System32\inetsrv folder.</p>
<p>To access IIS7, we use the ServerManager class, as shown below.</p>
<div class="csharpcode">
<pre><span class="lnum"> 1: </span><span class="kwrd">using</span> System;
<span class="lnum"> 2: </span><span class="kwrd">using</span> System.Collections.Generic;
<span class="lnum"> 3: </span><span class="kwrd">using</span> System.Linq;
<span class="lnum"> 4: </span><span class="kwrd">using</span> System.Text;
<span class="lnum"> 5: </span><span class="kwrd">using</span> Microsoft.Web.Administration;
<span class="lnum"> 6: </span>
<span class="lnum"> 7: </span><span class="kwrd">namespace</span> Iis7ManagementTest
<span class="lnum"> 8: </span>{
<span class="lnum"> 9: </span>  <span class="kwrd">class</span> Program
<span class="lnum"> 10: </span>  {
<span class="lnum"> 11: </span>    <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
<span class="lnum"> 12: </span>    {
<span class="lnum"> 13: </span>      ServerManager mgr = <span class="kwrd">new</span> ServerManager();
<span class="lnum"> 14: </span>
<span class="lnum"> 15: </span>      <span class="rem">// Add a new application pool called MyAppPool</span>
<span class="lnum"> 16: </span>      ApplicationPool myAppPool = mgr.ApplicationPools.Add(<span class="str">"MyAppPool"</span>);
<span class="lnum"> 17: </span>
<span class="lnum"> 18: </span>      <span class="rem">// Configure my new app pool to start automatically.</span>
<span class="lnum"> 19: </span>      myAppPool.AutoStart = <span class="kwrd">true</span>;
<span class="lnum"> 20: </span>
<span class="lnum"> 21: </span>      <span class="rem">// What action should IIS take when my app pool exceeds </span>
<span class="lnum"> 22: </span>      <span class="rem">// the CPU limit specified by the Limit property</span>
<span class="lnum"> 23: </span>      myAppPool.Cpu.Action = ProcessorAction.KillW3wp;
<span class="lnum"> 24: </span>
<span class="lnum"> 25: </span>      <span class="rem">// Use the Integrated Pipeline mode</span>
<span class="lnum"> 26: </span>      myAppPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
<span class="lnum"> 27: </span>
<span class="lnum"> 28: </span>      <span class="rem">// Set the runtime version of ASP.NET to 2.0</span>
<span class="lnum"> 29: </span>      myAppPool.ManagedRuntimeVersion = <span class="str">"v2.0"</span>;
<span class="lnum"> 30: </span>
<span class="lnum"> 31: </span>      <span class="rem">// Use the Network Service account</span>
<span class="lnum"> 32: </span>      myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
<span class="lnum"> 33: </span>
<span class="lnum"> 34: </span>      <span class="rem">// Shut down after being idle for 5 minutes.</span>
<span class="lnum"> 35: </span>      myAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
<span class="lnum"> 36: </span>
<span class="lnum"> 37: </span>      <span class="rem">// Max. number of IIS worker processes (W3WP.EXE)</span>
<span class="lnum"> 38: </span>      myAppPool.ProcessModel.MaxProcesses = 1;
<span class="lnum"> 39: </span>
<span class="lnum"> 40: </span>      <span class="rem">// Commit the changes</span>
<span class="lnum"> 41: </span>      mgr.CommitChanges();
<span class="lnum"> 42: </span>    }
<span class="lnum"> 43: </span>  }
<span class="lnum"> 44: </span>}</pre>
<p><span style="font-family: 'Trebuchet MS';">After the application has been executed, we will see our new application pool inside the IIS7 Manager:</span></p>
<p><a href="http://www.martinnormark.com/image.axd?picture=WindowsLiveWriter/AddingaVirtualDirectorytoIIS7programmati_124F2/image_4.png"><span style="font-family: tre; color: #000000;"><img style="border-width: 0px;" src="http://www.martinnormark.com/image.axd?picture=WindowsLiveWriter/AddingaVirtualDirectorytoIIS7programmati_124F2/image_thumb_1.png" alt="image" width="244" height="198" border="0" /></span></a></p>
<p><span style="font-family: 'Trebuchet MS';">That's all for now. In another blog post I will show how to add a new web site, that will use our new application pool - also from managed code.</span></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fAdding-an-Application-Pool-to-IIS7-programmatically.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fAdding-an-Application-Pool-to-IIS7-programmatically.aspx" alt="kick it on DotNetKicks.com" border="0" /></a></p>
</div>
<p>&nbsp;</p>
<div style="float: right;"><iframe style="width: 120px; height: 240px;" src="http://rcm.amazon.com/e/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS1=1&amp;nou=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=martnormsblog-20&amp;o=1&amp;p=8&amp;l=as1&amp;m=amazon&amp;f=ifr&amp;ref=tf_til&amp;asins=0470152532" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="320" height="240"></iframe></div>
<h3>The Integrated Programming model in IIS 7</h3>
<p>IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.</p>
<p>To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There's no limit to what you can do.</p>
<p>I found a lot of great stuff in the book called <a href="http://www.amazon.com/gp/product/0470152532/ref=as_li_tf_tl?ie=UTF8&amp;tag=martnormsblog-20&amp;linkCode=as2&amp;camp=217145&amp;creative=399381&amp;creativeASIN=0470152532">Professional IIS 7 and ASP.NET Integrated Programming</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=martnormsblog-20&amp;l=as2&amp;o=1&amp;a=0470152532&amp;camp=217145&amp;creative=399381" alt="" width="1" height="1" border="0" /> and learned a lot of useful stuff.</p>
<p>IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Configuring IIS 7 default document from web.config</title>
		<link>http://martinnormark.com/configuring-iis-7-default-document-from-web-config?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/configuring-iis-7-default-document-from-web-config#comments</comments>
		<pubDate>Thu, 20 Dec 2007 20:14:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/Configuring-IIS-7-default-document-from-webconfig.aspx</guid>
		<description><![CDATA[IIS 7 has a lot of changes regarding the configuration model. Using the Integrated Configuration System in IIS 7 you can set configuration of your server on different levels, compared to IIS 6, where the ASP.NET developer were more restricted. &#8230; <p><a class="btn small" href="http://martinnormark.com/configuring-iis-7-default-document-from-web-config">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>IIS 7 has a lot of changes regarding the configuration model. Using the Integrated Configuration System in IIS 7 you can set configuration of your server on different levels, compared to IIS 6, where the ASP.NET developer were more restricted. Configuring IIS 7 from an ASP.NET Web Application's web.config file works from both ASP.NET 2.0 and ASP.NET 3.5. Note that the server can be restricted to lock certain configuration settings from above the application level, you might encounter this if your website runs in a shared hosting environment.</p>
<p><strong>Setting up the website</strong></p>
<p>To have a test website, I will setup a new website on my Vista machine running IIS 7. I don't want the website to run under the Default Web Site, so first thing, I'll add a hostname to my hosts file. This is located in the drivers\etc folder of your windows directory's system32 folder. Add the following line:</p>
<p>127.0.0.1         iis7test</p>
<p>Open up Internet Information Services (IIS) Manager, and create a new Application Pool. Name it iis7test, and leave the settings as default:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_2.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb.png" border="0" alt="image" width="278" height="250" /></a></p>
<p>Add a new website with the following settings:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_4.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb_1.png" border="0" alt="image" width="454" height="420" /></a></p>
<p>This should get us started. Open up Visual Studio 2008 (2005 will also work). Open the website you just created inside IIS.</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_6.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb_2.png" border="0" alt="image" width="454" height="362" /></a></p>
<p>The only item in the website is a web.config. Add a new Web Form called Test.aspx.</p>
<p>Right-click the Test.aspx file and click View in browser. You could get an HTTP Error 500.00, like this:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_8.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb_3.png" border="0" alt="image" width="454" height="328" /></a></p>
<p>To get rid of it, set impersonate to false in the web.config file by adding this line to the system.web section:</p>
<p>&lt;identity impersonate="false" /&gt;</p>
<p>Now your site should work fine. Edit the Test.aspx file, so it has some content. Just write Test IIS 7 or something in the HTML mark-up.</p>
<p>If you right-click the website icon in the Solution Explorer, and click View in browser, you will get an error like the one bellow:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_10.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb_4.png" border="0" alt="image" width="454" height="502" /></a></p>
<p>That is because our website does not have any pages that match the name of the default documents on IIS. If you don't want to be limited by the few default documents that comes with IIS out-of-the-box, you can add your own from web.config by adding these lines:</p>
<p>&lt;system.webServer&gt;<br />
&lt;defaultDocument&gt;<br />
&lt;files&gt;<br />
&lt;clear /&gt;<br />
&lt;add value="Test.aspx" /&gt;<br />
&lt;/files&gt;<br />
&lt;/defaultDocument&gt;<br />
&lt;/system.webServer&gt;</p>
<p>Maybe the system.webServer section already existed in your web.config file. If so, just add the defaultDocument section. Here you can specify all the filenames that should act as a default document in IIS 7. The &lt;clear /&gt; line is optional. If you want to keep the default settings, and only add your own - just delete that line.</p>
<p>Now when we save the new web.config file, and refresh the browser we se our Test.aspx file:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_12.png"><img style="border: 0px;" src="/image.axd?picture=WindowsLiveWriter/ConfiguringIIS7defaultdocumentfro.config_126D3/image_thumb_5.png" border="0" alt="image" width="244" height="191" /></a></p>
<p>This setting is just one of many that you can specify in your application's web.config file. In the near future I'll be blogging more about specific settings you can use to configure IIS 7 from ASP.NET.</p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:31745098-9c0f-41f3-b271-f61a56fcdf5f" class="wlWriterSmartContent" style="display: inline; margin: 0px; padding: 0px;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/ASP.NET">ASP.NET</a>, <a rel="tag" href="http://technorati.com/tags/IIS7">IIS7</a></div>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fConfiguring-IIS-7-default-document-from-webconfig.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fConfiguring-IIS-7-default-document-from-webconfig.aspx" border="0" alt="kick it on DotNetKicks.com" width="82" height="18" /></a></p>
<div style="float: right;">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS1=1&#038;nou=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=martnormsblog-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;ref=tf_til&#038;asins=0470152532" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<h3>The Integrated Programming model in IIS 7</h3>
<p>IIS 7 comes with a bunch of improvements for developers. You can do a whole lot of exciting things even from web.config, but also from code.</p>
<p>To take advantage of the powerful integrated programming model, you need to set your application pool to use the Integrated pipeline mode. There's no limit to what you can do.</p>
<p>I found a lot of great stuff in the book called <a href="http://www.amazon.com/gp/product/0470152532/ref=as_li_tf_tl?ie=UTF8&#038;tag=martnormsblog-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399381&#038;creativeASIN=0470152532">Professional IIS 7 and ASP.NET Integrated Programming</a><img src="http://www.assoc-amazon.com/e/ir?t=martnormsblog-20&#038;l=as2&#038;o=1&#038;a=0470152532&#038;camp=217145&#038;creative=399381" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and learned a lot of useful stuff.</p>
<p>IIS 7 (or 7.5) is still the preferred way to develop, test and deploy ASP.NET applications, and as a developer you must stay up-to-date on what your tools and framework has to offer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/configuring-iis-7-default-document-from-web-config/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>How to handle IIS Event id 1009 (Event id 5011 on IIS 7)</title>
		<link>http://martinnormark.com/how-to-handle-iis-event-id-1009-event-id-5011-on-iis-7?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/how-to-handle-iis-event-id-1009-event-id-5011-on-iis-7#comments</comments>
		<pubDate>Fri, 26 Oct 2007 17:57:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">/post/How-to-handle-IIS-Event-id-1009-(Event-id-5011-on-IIS-7).aspx</guid>
		<description><![CDATA[Recently at work we had some serious production environment issues. Our web application basically made the IIS application pool terminate unexpectedly. And we didn't get a single stack trace or a line of code. The only thing that was logged &#8230; <p><a class="btn small" href="http://martinnormark.com/how-to-handle-iis-event-id-1009-event-id-5011-on-iis-7">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Recently at work we had some serious production environment issues. Our web application basically made the IIS application pool terminate unexpectedly. And we didn't get a single stack trace or a line of code. The only thing that was logged inside of the Windows Event Viewer, was event id 1009, which basically told that the application pool serving our app terminated unexpectedly. And that was it. All active user sessions were wiped, and the users had to login again.</p>
<p>The error started after we did a major upgrade. A lot of our code had changed since the previous version, so we set up an new website and application pool inside IIS 6.0 for the new version, so we could upload the new application, and then turn off the old one, to get as seamless an upgrade as possible. It worked fine, and we were happy. Until a few hours later, when we saw our users were logged off.</p>
<p>At first we thought it was IIS settings we had done wrong, so we did a complete comparison with the old website and application pool to see if we forgot anything. There was no difference at all. After a while, we realized that the only thing that could make the IIS crash like that, was our own code. But with no clue of where the danger in our code were - we were lost.</p>
<p>After some time, we got WinDbg attached to the worker process of IIS, serving our application. We caught a few memory dumps, but those were not of the exception we were looking for.</p>
<p>Later I found an HttpModule for ASP.NET. Basically the .Net framework 2.0 has changed the way it handles exceptions from other threads in IIS. In ASP.NET 1.1, unhandled exceptions from asynchronous threads inside IIS were ignored. But in ASP.NET 2.0, those unhandled exceptions makes the IIS application pool crash.</p>
<h3>Make the IIS crash yourself</h3>
<p>So try to do this. Create a new ASP.NET 2.0 website running on your local IIS. You only need a single page, so you're fine with the default.aspx Visual Studio creates for you. Add a button to the page, and create an event handler for the buttons OnClick event.</p>
<p>Add this to your code-behind:</p>
<pre class="csharpcode">  <span class="rem">/// &lt;summary&gt;</span>
  <span class="rem">/// Handles the Click event of the btnMakeCrash control.</span>
  <span class="rem">/// &lt;/summary&gt;</span>
  <span class="rem">/// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;</span>
  <span class="rem">/// &lt;param name="e"&gt;The EventArgs instance containing the event data.&lt;/param&gt;</span>
  <span class="kwrd">protected</span> <span class="kwrd">void</span> btnMakeCrash_Click(<span class="kwrd">object</span> sender, EventArgs e)
  {
    ThreadPool.QueueUserWorkItem(<span class="kwrd">new</span> WaitCallback(MakeIisCrash));
  }

  <span class="rem">/// &lt;summary&gt;</span>
  <span class="rem">/// Makes the IIS crash.</span>
  <span class="rem">/// &lt;/summary&gt;</span>
  <span class="rem">/// &lt;param name="stateInfo"&gt;The state info.&lt;/param&gt;</span>
  <span class="kwrd">private</span> <span class="kwrd">void</span> MakeIisCrash(<span class="kwrd">object</span> stateInfo)
  {
    <span class="rem">// Instantiate the DataSet to null</span>
    DataSet ds = <span class="kwrd">null</span>;

    <span class="rem">// Make an unhandled NullReferenceException</span>
    ds.CaseSensitive = <span class="kwrd">true</span>;
  }</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>The buttons click event handler uses the ThreadPool to execute the MakeIisCrash() method. This method instantiates a DataSet as null, and the sets a property. Since the DataSet is null, this throws a NullReferenceException which is not handled, as you can see.</p>
<p>Click the button, and see how IIS will crash.</p>
<p>You can sense that your computer is working harder - that is because IIS terminates the application pool. If you go and check the Windows Application log, you can see that is has added an error:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/HowtohandleIISEventid1009Eventid5001onII_59F/image_2.png"><img style="border-width: 0px;" src="/image.axd?picture=WindowsLiveWriter/HowtohandleIISEventid1009Eventid5001onII_59F/image_thumb.png" border="0" alt="image" width="580" height="527" /></a></p>
<p>Now this doesn't tell you much. Imagine if you had hundreds of thousands line of code - how would you find the cause of the error?</p>
<p>Inside Visual Studio, add a new class to the website - call it UnhandledExceptionModule, and apply this code:</p>
<pre class="csharpcode"><span class="preproc">#region</span> Using

<span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Diagnostics;
<span class="kwrd">using</span> System.Globalization;
<span class="kwrd">using</span> System.IO;
<span class="kwrd">using</span> System.Runtime.InteropServices;
<span class="kwrd">using</span> System.Text;
<span class="kwrd">using</span> System.Threading;
<span class="kwrd">using</span> System.Web;

<span class="preproc">#endregion</span>

<span class="rem">/// &lt;summary&gt;</span>
<span class="rem">/// Handles all unhandled exceptions from in the current AppDomain. </span>
<span class="rem">/// </span>
<span class="rem">/// Works great to catch unhandled exceptions thrown by IIS's child threads, </span><span class="rem">/// <span class="rem">which will make the </span>application pool terminate unexpectedly 

/// <span class="rem">without logging. This makes sure your Exception</span> </span><span class="rem">/// is logged to the Application event log.</span>
<span class="rem">/// &lt;/summary&gt;</span>
<span class="kwrd">public</span> <span class="kwrd">class</span> UnhandledExceptionModule : IHttpModule
{
  <span class="preproc">#region</span> Fields

  <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> _UnhandledExceptionCount = 0;
  <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span> _SourceName = <span class="kwrd">null</span>;
  <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">object</span> _InitLock = <span class="kwrd">new</span> <span class="kwrd">object</span>();
  <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">bool</span> _Initialized = <span class="kwrd">false</span>;

  <span class="preproc">#endregion</span>

  <span class="preproc">#region</span> IHttpModule members

  <span class="kwrd">public</span> <span class="kwrd">void</span> Init(HttpApplication app)
  {

    <span class="rem">// Do this one time for each AppDomain.</span>
    <span class="kwrd">if</span> (!_Initialized)
    {
      <span class="kwrd">lock</span> (_InitLock)
      {
        <span class="kwrd">if</span> (!_Initialized)
        {
          <span class="kwrd">string</span> webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), <span class="str">"webengine.dll"</span>);

          <span class="kwrd">if</span> (!File.Exists(webenginePath))
          {
            <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(String.Format(CultureInfo.InvariantCulture,                 <span class="str">"Failed to locate webengine.dll at '{0}'.  This module requires .NET Framework 2.0."</span>, webenginePath));
          }

          FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
          _SourceName = <span class="kwrd">string</span>.Format(CultureInfo.InvariantCulture, <span class="str">"ASP.NET {0}.{1}.{2}.0"</span>, ver.FileMajorPart,                 ver.FileMinorPart, ver.FileBuildPart);

          <span class="kwrd">if</span> (!EventLog.SourceExists(_SourceName))
          {
            <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(String.Format(CultureInfo.InvariantCulture,                 <span class="str">"There is no EventLog source named '{0}'. This module requires .NET Framework 2.0."</span>, _SourceName));
          }

          AppDomain.CurrentDomain.UnhandledException += <span class="kwrd">new</span> UnhandledExceptionEventHandler(OnUnhandledException);

          _Initialized = <span class="kwrd">true</span>;
        }
      }
    }
  }

  <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()
  {
  }

  <span class="preproc">#endregion</span>

  <span class="preproc">#region</span> UnhandledException <span class="kwrd">event</span> handler

  <span class="kwrd">public</span> <span class="kwrd">void</span> OnUnhandledException(<span class="kwrd">object</span> o, UnhandledExceptionEventArgs e)
  {
    <span class="rem">// Let this occur one time for each AppDomain.</span>
    <span class="kwrd">if</span> (Interlocked.Exchange(<span class="kwrd">ref</span> _UnhandledExceptionCount, 1) != 0)
      <span class="kwrd">return</span>;

    StringBuilder message = <span class="kwrd">new</span> StringBuilder(<span class="str">"\r\n\r\nUnhandledException logged by UnhandledExceptionModule:\r\n\r\nappId="</span>);

    <span class="kwrd">string</span> appId = (<span class="kwrd">string</span>)AppDomain.CurrentDomain.GetData(<span class="str">".appId"</span>);
    <span class="kwrd">if</span> (appId != <span class="kwrd">null</span>)
    {
      message.Append(appId);
    }

    Exception currentException = <span class="kwrd">null</span>;
    <span class="kwrd">for</span> (currentException = (Exception)e.ExceptionObject; currentException != <span class="kwrd">null</span>; currentException = currentException.InnerException)
    {
      message.AppendFormat(<span class="str">"\r\n\r\ntype={0}\r\n\r\nmessage={1}\r\n\r\nstack=\r\n{2}\r\n\r\n"</span>,
                           currentException.GetType().FullName,
                           currentException.Message,
                           currentException.StackTrace);
    }

    EventLog Log = <span class="kwrd">new</span> EventLog();
    Log.Source = _SourceName;
    Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
  }

  <span class="preproc">#endregion</span>
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>Modify the httpModules section inside web.config to look lige this:</p>
<pre class="csharpcode">        &lt;httpModules&gt;            &lt;add type=<span class="str">"UnhandledExceptionModule"</span> name=<span class="str">"UnhandledExceptionModule"</span>/&gt;        &lt;/httpModules&gt;</pre>
<p>This is your new friend. It is a must for any ASP.NET 2.0 application running in production environment. This will catch your exception, and write the message and the stack trace to the Windows Application Event log, and now you can see what caused the crash:</p>
<p><a href="/image.axd?picture=WindowsLiveWriter/HowtohandleIISEventid1009Eventid5001onII_59F/image_4.png"><img style="border-width: 0px;" src="/image.axd?picture=WindowsLiveWriter/HowtohandleIISEventid1009Eventid5001onII_59F/image_thumb_1.png" border="0" alt="image" width="592" height="539" /></a></p>
<p>So after we applied this HttpModule to our production environment, we get a nice entry in our Application event log from ASP.NET. It includes a stack trace, which makes us able to find the problem, and fix it!</p>
<p>Further reading regarding ASP.NET production environment issues:</p>
<ul>
<li><a href="http://blogs.msdn.com/tess/default.aspx">Tess Ferrandez</a> - She has a lot of great posts about debugging ASP.NET using <a href="http://en.wikipedia.org/wiki/WinDbg">WinDbg</a></li>
<li><a href="http://en.wikipedia.org/wiki/WinDbg">WinDbg</a></li>
<li><a href="http://blogs.msdn.com/david.wang/default.aspx">David Wang</a> - <a href="http://blogs.msdn.com/david.wang/archive/2005/08/29/HOWTO-Understand-and-Diagnose-an-AppPool-Crash.aspx">HOWTO: Understand and Diagnose an Application Pool Crash</a></li>
<li><a href="http://dotnetdebug.net/">Advanced .NET debugging</a></li>
</ul>
<p>Hope this will help someone.</p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:751a45b8-668d-4e02-84c9-59c551706890" class="wlWriterSmartContent" style="display: inline; margin: 0px; padding: 0px;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/ASP.NET">ASP.NET</a>, <a rel="tag" href="http://technorati.com/tags/Debugging">Debugging</a>, <a rel="tag" href="http://technorati.com/tags/IIS">IIS</a>, <a rel="tag" href="http://technorati.com/tags/.NET">.NET</a></div>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fHow-to-handle-IIS-Event-id-1009-(Event-id-5011-on-IIS-7).aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.martinnormark.com%2fpost%2fHow-to-handle-IIS-Event-id-1009-(Event-id-5011-on-IIS-7).aspx" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/how-to-handle-iis-event-id-1009-event-id-5011-on-iis-7/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

