如果我想使用 Microsoft.Web.Administration,相当于 IISReset 的是什么

发布于 2024-12-29 23:15:25 字数 189 浏览 1 评论 0原文

看起来,除了迭代 ApplicationPools 并对其调用 Recycle() 之外,还涉及更多内容。如何以编程方式(来自 .Net)最接近地模仿 IISReset,而不诉诸 shell 调用?

我认为最重要的是: IISReset 所做的事情是否无法通过 Microsoft.Web.Administration 命名空间中提供的工具来完成?

It would appear that there is more involved than just iterating the ApplicationPools and calling Recycle() on them. How do I most closely mimic an IISReset programatically (from .Net) without resorting to shell invocation?

And I suppose most importantly: Are there things that IISReset does that can't be accomplished via the tools available in Microsoft.Web.Administration namespace?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2025-01-05 23:15:25

您可以使用 System.ServiceProcess.ServiceController (从这个 答案),通过启动或停止以下服务:

-Windows 进程激活服务 (WAS)
- 万维网发布服务 (W3SVC)

以下是一些可以完成这项工作的代码:

//stop iis (like running "IISReset /Stop")
ResetService("WAS", false);
ResetService("W3SVC", false);

//start iis (like running "IISReset /Start")
ResetService("WAS", true);
ResetService("W3SVC", true);

private static void ResetService(string name, bool start)
{
    using (var service = new System.ServiceProcess.ServiceController(name))
    {
        if (start && service.Status == ServiceControllerStatus.Stopped)
        {
            service.Start();
        }
        else if (!start && service.Status == ServiceControllerStatus.Running)
        {
            service.Stop();
        }
    }
 }

至于其他 IISReset 命令,您可以轻松地编写超时代码。要重新启动计算机,请查看此答案。如果您需要更多详细信息,请告诉我。

但是,如果这对您来说还不够,您始终可以使用 这个技巧(漂亮的球手,如果你只需要完成任务)。

You can use System.ServiceProcess.ServiceController (got the idea from this answer), by starting or stopping the following services:

-Windows Process Activation Service (WAS)
-World Wide Web Publishing Service (W3SVC)

Here's some code that'll do the job:

//stop iis (like running "IISReset /Stop")
ResetService("WAS", false);
ResetService("W3SVC", false);

//start iis (like running "IISReset /Start")
ResetService("WAS", true);
ResetService("W3SVC", true);

private static void ResetService(string name, bool start)
{
    using (var service = new System.ServiceProcess.ServiceController(name))
    {
        if (start && service.Status == ServiceControllerStatus.Stopped)
        {
            service.Start();
        }
        else if (!start && service.Status == ServiceControllerStatus.Running)
        {
            service.Stop();
        }
    }
 }

As for other IISReset commands, you could code in a timeout easily enough. And to reboot the computer, check out this answer. Let me know if you need any more details.

But if that's not enough of a party for ye, you can always execute power shell scripts in c#, using this technique (pretty baller, if you just need to get 'er done).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文