This blog is used as a memory dump of random thoughts and interesting facts about different things in the world of IT. If anyone finds it useful, the author will be just happy! :-)

Tuesday, January 20, 2009

IIS extension: WebAppPool

Another challenge - another piece of fun with WiX. Imagine the following requirement: the installation program must install an application pool on IIS6+ environments; the multiple installed instances should use the same application pool. In other words, the application pool must be created with the first instance installation, and must be removed with the last instance uninstallation. 

A special element for maintaining IIS AppPools in IIS extension is called WebAppPool. As usual, we'll wrap it into a separate component, so that it is created on install. Later, we'll create a special custom action to deceive the standard removing mechanism on uninstall:

      <Component DiskId="1" Id="CreateIISAppPool" Guid="{YOURGUID-6C5B-4980-AD0B-E32FA2DBC1F4}" Directory="WebsiteFolder">
         <Condition>IISMAJORVERSION <> "#5"</Condition>
         <iis:WebAppPool Id="IISSiteAppPool6" Name="[IISAPPPOOL_NAME]" MaxWorkerProcesses="1" Identity="networkService" />
         <RegistryKey Root="HKLM" Key="$(var.ParentKey)">
            <RegistryValue Name="IISAppPoolName" Type="string" Value="[IISAPPPOOL_NAME]"/>

As you can see, the component is installed once the target system has IIS 6+. It creates a WebAppPool with the name provided in IISAPPPOOL_NAME public property. It also writes this name into a registry value, which resides under the instance-specific registry key. 
With this component included into the MSI package, the app pool is created when the first instance is installed, and nothing happens for second and subsequent instances. 

Let's examine the uninstall behavior. The MSI behaves natural - when it meets the component to uninstall, it removes the WebAppPool specified in it. But the IIS extension which performs the actual deletion of app pool, needs the name to be passed in it. So, the only thing we should do is to supply this action with a fake app pool name each time, except for the last instance uninstall.

Here is the algorithm:
  1. search the registry for the app pool name as usual
  2. schedule a special action on unistall after AppSearch, which detects if this is the last instance being uninstalled, and if not, "breaks" the app pool name into something non-existent
The first point is quite straight-forward:

      <Property Id="IISAPPPOOL_NAME">
         <RegistrySearch Id="IISAppPoolName" Root="HKLM" Key="$(var.ParentKey)" Name="IISAppPoolName" Type="raw" />

The second one is not natural, like any hack:

      public static ActionResult ChangeWebAppPoolNameToDeceiveUninstall(Session session)
         int numberOfInstalled = 1;
         foreach (ProductInstallation product in ProductInstallation.GetRelatedProducts(session["UpgradeCode"]))
            if ((session["ProductCode"] != product.ProductCode) && product.IsInstalled)

         if (numberOfInstalled > 1)
            session["IISAPPPOOL_NAME"] += string.Format("|{0}", DateTime.Now.ToLongTimeString());

         return ActionResult.Success;

It iterates the related products (those sharing the UpgradeCode), and if it finds others installed, except for itself, it changes the app pool name we retrieved from registry into something unique, for instance, appends a unique string.

Thus, the IIS custom action which is going to delete the app pool fails to find the one with the provided name, and does nothing. When, otherwise, it is the last instance being uninstalled, the retrieved app pool name remains unchanged, and the app pool is successfully removed.

Note that the mentioned action should be immediate, should occur after AppSearch on uninstall.

That's it! I would appreciate any comments as usual.


  1. Buy without a bank There are many legal requirements when buying a house without a bank.i can buy any things and any property without any help of any bank .This Is very nice blog .

  2. The salon supplies and its new features are surprising, cutting-edge and truly useful enhancements.Next living room furniture Home living Stores contemporary range of furniture beds & home style.

  3. A temporary structure on the outside of a building, made of wooden planks and metal poles, used by workmen while building, repairing, or cleaning the building.the materials used in Scaffolding .This blog is very useful and helpful.

  4. Thanks for sharing in detail. Your blog is an inspiration! Apart of really useful tips, it's just really !

    Cyprus property

  5. Good line and nice image this post. this post will be effectively Just about everything looks good displayed thanks for sharing.
    Land selling