简体   繁体   English

从自己的线程停止IIS网站

[英]Stop IIS website from its own thread

I am using the following code to stop my WCF service from its own thread to update some files that are used by my service. 我正在使用以下代码从其自己的线程停止WCF服务,以更新服务使用的某些文件。

try
{
    var server = new ServerManager();
    var site = server.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
    if (site != null)
    {
        Thread.Sleep(1000);
        site.Stop();
        if (site.State == ObjectState.Stopped)
        {
            Thread.Sleep(5000);
        }
        site.Start();
    }
    else
    {
        throw new FaultException("Server Are Trying To Stop Is not Found");
    }
}
catch (Exception ex)
{
    throw new FaultException(ex.Message);
}

But I get following error when I execute the code: 但是执行代码时出现以下错误:

"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" “访问被拒绝。(HRESULT的异常:0x80070005(E_ACCESSDENIED))”

I think you should run Powershell script for such case, as you want to stop the process which is self as I understand. 我想您应该针对这种情况运行Powershell脚本,因为您想停止该进程,据我所知。 Meaning that after stop, your process will be killed and no update can be performed. 这意味着停止后,您的进程将被杀死,无法执行任何更新。 With power shell you could stop process, copy over files and start over 使用Power Shell,您可以停止进程,复制文件并重新开始

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
#... copy files here
Start-WebSite 'Default Web Site'

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

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