简体   繁体   中英

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.

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))"

I think you should run Powershell script for such case, as you want to stop the process which is self as I understand. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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