在 IIS7 中以编程方式安装 ISAPI 扩展

发布于 2024-09-25 11:58:38 字数 2470 浏览 9 评论 0原文

我想以编程方式在Windows7上的IIS7中安装ISAPI扩展

(控制面板->程序和功能->打开或关闭Windows功能->IIS->WWW->应用程序开发功能->ISAPI扩展)。

基本上我想实现以下 DISM 命令

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

我可以做的事情我尝试使用 Microsoft.Web.Administration,以下是代码


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();
}

它确实更新了 applicationHost.config。当我比较用我的代码更新的 applicationHost.config 和用 DISM 更新的 applicationHost.config 时,它们几乎是相同的。唯一的区别是,上面的代码没有在模块部分设置“lockItem=true”,因为这样写会出错。但我认为这应该不重要。

但上面的代码没有安装该功能,而 DISM 却安装了。有什么区别?

我做了另一个测试。

1) 使用 DISM 启用 ISAPI 扩展 2) 保存 applicationHost.config 的副本 3) 使用 DISM 禁用 ISAPI 扩展 4) 用保存的副本覆盖 apllicationHost.config

它不会安装 ISAPI 扩展功能。因此 DISM 不仅会更新 applicationHost.config,还会执行更多操作。那么它有什么作用以及如何以编程方式实现相同的目的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文