简体   繁体   English

是否可以以编程方式在IIS中启动/停止应用程序池或网站?

[英]Is it possible to start / stop an application pool or website in IIS programatically?

I'm trying to find a way to stop, start, restart/recycle websites and application pools from within a web interface on the same server. 我正在尝试从同一服务器上的Web界面中找到一种停止,启动,重新启动/回收网站和应用程序池的方法。 Ideally this would be something I could do with .NET without having to execute shell commands. 理想情况下,这是我可以用.NET做的事情而不必执行shell命令。

(I see other related questions have been asked but I don't want to do this from the command line unless that's necessary). (我看到其他相关问题已经被问到,但我不希望从命令行执行此操作,除非这是必要的)。

I need to be able to do this for both IIS6 and IIS7. 我需要能够为IIS6和IIS7执行此操作。

You are looking for the Microsoft.Web.Administration.ServerManager class. 您正在寻找Microsoft.Web.Administration.ServerManager类。 You can find some starter code here: http://learn.iis.net/page.aspx/226/microsoftwebadministration/ . 您可以在此处找到一些入门代码: http//learn.iis.net/page.aspx/226/microsoftwebadministration/

You can explore that code further to do what you want. 您可以进一步探索该代码以执行您想要的操作。 I believe the whole appcmd is written on top of this framework. 我相信整个appcmd都是在这个框架之上编写的。

This framework will not work for IIS6 and below. 此框架不适用于IIS6及更低版本。 You need IIS7. 你需要IIS7。 For II6 you could use the WMI approach as suggested by Mehmet Aras. 对于II6,您可以使用Mehmet Aras建议的WMI方法。 It is unfortunate, but you need these two libraries; 不幸的是,你需要这两个库; or maybe there is some code out there that abstracts these libraries. 或者可能有一些代码抽象这些库。

You can use WMI (Windows Management Instrumentation). 您可以使用WMI(Windows Management Instrumentation)。 Take a look at Using WMI to Configure IIS on msdn.You can also access WMI providers and interact with them including IIS from C# code. 在msdn上查看使用WMI配置IIS 。您还可以访问WMI提供程序并与它们进行交互,包括来自C#代码的IIS。 System.Management namespace is a place to start. System.Management命名空间是一个可以开始的地方。

One thing I would caution you is to find out if the WMI code in .NET requires full-trust or not since you want to be able to do this from within a .NET web application which does not run in full-trust by default. 我要提醒你的一件事是要弄清楚.NET中的WMI代码是否需要完全信任,因为你希望能够在.NET Web应用程序中执行此操作,该应用程序默认情况下不会以完全信任方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM