Restarting the application pool using Nant

5. April 2008

I've had times where CruiseControl would not build because files were in use. This is despite the fact that I stop IIS prior to deploying files. So, I modified my deployment build script to also recycle the application pool. This solution builds on information from Artur Carvalho's blog.

 

<property name="appPoolName" value="DefaultAppPool" readonly="false" overwrite="true" />
<script language="C#">
    <references>
      <include name="System.DirectoryServices.dll" />
    </references>        
    <imports>
        <import namespace="System.DirectoryServices" />
    </imports>
    <code>
        <![CDATA[
        public static void ScriptMain( Project project )
        {
            string appPoolName = project.Properties["appPoolName"];
            DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            DirectoryEntry newpool = apppools.Children.Find(appPoolName,"IIsApplicationPool");
            newpool.Invoke("stop",new object[]{});
            newpool.Invoke("start",new object[]{});
            newpool.CommitChanges();
        }
        ]]>
    </code>
</script>

 

.NET Development

blog comments powered by Disqus