views:

31

answers:

0

Hi,

I want to install ISAPI extensions in IIS7 on Windows7 programmatically

(Control Panel -> Programs and features->Turn windows features on or off->IIS->WWW->Application Development feature->ISAPI Extensions).

Basically I want to achieve what I can do with following DISM command

dism /online /Enable-Feature /FeatureName:IIS-ISAPIExtensions

I tried to use Microsoft.Web.Administration, following is the code


using (ServerManager serverManager = new ServerManager())
{
   Configuration config = serverManager.GetApplicationHostConfiguration();
   ConfigurationSection gloabalmodulesSection = config.GetSection("system.webServer/globalModules");
   ConfigurationElementCollection globalModulesCollection = gloabalmodulesSection.GetCollection();
   ConfigurationElement isapiExtension = globalModulesCollection.CreateElement("add");
   isapiExtension.SetAttributeValue("name", "IsapiModule");
   isapiExtension.SetAttributeValue("image", "%windir%\\System32\\inetsrv\\isapi.dll");
   globalModulesCollection.Add(isapiExtension);

   ConfigurationSection modulesSection = config.GetSection("system.webServer/modules");
   ConfigurationElementCollection modulesCollection = modulesSection.GetCollection();
   ConfigurationElement isapiModule = modulesCollection.CreateElement("add");
   isapiModule.SetAttributeValue("name", "IsapiModule");
   modulesCollection.AddAt(10,isapiModule);

   ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers");
   ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();
   ConfigurationElement isapiHandler = handlersCollection.CreateElement("add");
   isapiHandler.SetAttributeValue("name", "ISAPI-dll");
   isapiHandler.SetAttributeValue("path", "*.dll");
   isapiHandler.SetAttributeValue("verb", "*");
   isapiHandler.SetAttributeValue("modules", "IsapiModule");
   isapiHandler.SetAttributeValue("resourceType","File");
   isapiHandler.SetAttributeValue("requireAccess","Execute");
   isapiHandler.SetAttributeValue("allowPathInfo","true");
   handlersCollection.AddAt(0, isapiHandler);
   serverManager.CommitChanges();
}

It does update the applicationHost.config. When I compare the applicationHost.config updated with my code and the same being updated with DISM they are almost same. The only difference is, the above code does not set "lockItem=true" in module section because writing so gives error. But I think it should not matter.

But the above code does not install the feature and DISM does. What is the difference ?

There is another test I did.

1) Enabled ISAPI extensions with DISM 2) Saved a copy of applicationHost.config 3) Disabled ISAPI extensions with DISM 4) Overwritten the apllicationHost.config with saved copy eariler

It does not install the ISAPI extension feature. So DISM not only updates the applicationHost.config but does something more. So what does it do and how can I achieve the same programmatically?