2017-11-22 16 views
0

Ich bin nicht in der Lage, die Klasse zu finden, die verwendet wird, um den Web-Service auf Remote-Server mit einem Code wie wir für Window-Service haben zu starten.Wie Web Service auf Remote-Server pragmatisch in c neu starten #

 var sc = new System.ServiceProcess.ServiceController("mywebsite", "remoteservername"); 
     sc.Start(); 
     sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running); 
     sc.Stop(); 
     sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped); 
+0

Wer kann mir helfen? –

+0

hast du meinen Vorschlag versucht? – Yuri

Antwort

0

static void Main (string [] args) {

 try 
     { 
     using (ServerManager manager = new ServerManager()) 
     { 
      var iisManager = ServerManager.OpenRemote("YourServerName"); 

      Microsoft.Web.Administration.Site site = iisManager.Sites.Where(q => q.Name.Equals("YourSiteName")).FirstOrDefault(); 

      if (site.State== site.Start()) 
      { 
       site.Stop(); 
      } 
      else 
      { 
       site.Start(); 
      } 

      manager.CommitChanges(); 


      } 

     } 
     catch (Exception ex) 
      { 

      } 
    } 
1

Der Webdienst ist nicht unter Windows-Dienste aufgeführt. Es läuft unter IIS und um es zu stoppen/zu starten, müssen Sie den Anwendungspool stoppen/starten, unter dem dieser Dienst läuft. Wenn Sie dies remote planen, muss WMI auf dem Zielserver aktiviert sein. Für Ihre Bequemlichkeit einen Code bereitstellt, die dies für Sie tun:

public void PoolAction(String servername, String AppPoolName, String action) 
    { 
     StringBuilder sb = new StringBuilder(); 
     ConnectionOptions options = new ConnectionOptions(); 
     options.Authentication = AuthenticationLevel.PacketPrivacy; 
     options.EnablePrivileges = true; 
     ManagementScope scope = new ManagementScope(@"\\" + 
      servername + "\\root\\MicrosoftIISv2", options); 

     // IIS WMI object IISApplicationPool to perform actions on IIS Application Pool 
     ObjectQuery oQueryIISApplicationPool = 
      new ObjectQuery("SELECT * FROM IISApplicationPool"); 

     ManagementObjectSearcher moSearcherIISApplicationPool = 
      new ManagementObjectSearcher(scope, oQueryIISApplicationPool); 
     ManagementObjectCollection collectionIISApplicationPool = 
      moSearcherIISApplicationPool.Get(); 
     foreach (ManagementObject resIISApplicationPool in collectionIISApplicationPool) 
     { 
      if (resIISApplicationPool["Name"].ToString().Split('/')[2] == AppPoolName) 
      { 
       // InvokeMethod - start, stop, recycle can be passed as parameters as needed. 
       resIISApplicationPool.InvokeMethod(action, null); 
      } 
     } 

Hinweis:

  • Aktion enthalten 'Start' können 'Stop' oder 'Recycle'
  • Konto unter denen Dieser Code muss auf dem Zielserver admin sein.

Wie WMI auf dem Server aktivieren enabling WMI on the server