Stop IIS site from own thread

I use the following code to stop my WCF service from my thread, to update some files that my service uses.

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 the following error while executing the code:

"Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

0
source share
1 answer

I think you should run the Powershell script for this case, since you want to stop the process, which in itself, as I understand it. This means that after stopping, your process will be killed and the update will not be completed. With the power shell you can stop the process, copy files and get started

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

All Articles