2012-07-09 72 views
4

我們有一個服務,我們創建(並在我們的服務器上運行),我想通過另一個程序來控制。我雖然服務有些困難,但希望有人能幫忙。一旦程序關閉,遠程服務關閉

如果我嘗試使用下面的路線來控制服務,它僅適用於在服務器上,以及用戶:

ServiceController service = new ServiceController("MyService", "MyServer"); 

try 
{ 
    if (service.Status == ServiceControllerStatus.Running) 
    { 
     teServiceStatus.BackColor = Color.LightGreen; 
     teServiceStatus.Text = "Running"; 
     sbStartService.Enabled = false; 
     sbStopService.Enabled = true; 
    } 
    else 
    { 
     teServiceStatus.BackColor = Color.Red; 
     teServiceStatus.Text = "Stopped"; 
     sbStartService.Enabled = true; 
     sbStopService.Enabled = false; 
    } 
} 
catch (InvalidOperationException ioe) 
{ 
    if (System.Diagnostics.Debugger.IsAttached) 
     System.Diagnostics.Debugger.Break(); 
    else 
    { 
     teServiceStatus.Text = "Cannot Connect"; 
     teServiceStatus.BackColor = Color.Red; 
    } 
} 
catch (Exception ex) 
{ 
    if (System.Diagnostics.Debugger.IsAttached) 
     System.Diagnostics.Debugger.Break(); 
    else 
     ; 
} 

所以,作爲一個網絡用戶,我也是服務器作爲上用戶,當我運行我們想要控制這個服務的程序時,我可以看到狀態並啓動並停止它,但沒有其他人可以(除非我將它們添加到服務器,我不希望這樣做)。

我還可以使用路線的下方,模擬用戶在服務器上:

//Establish connection to the Publishing Server with application credentials. 
ConnectionOptions connectionOptions = new ConnectionOptions(); 
connectionOptions.Impersonation = ImpersonationLevel.Impersonate; 
connectionOptions.EnablePrivileges = true; 
connectionOptions.Username = "UserOnServer"; 
connectionOptions.Password = "UserPassword"; 
ManagementScope managementScope = new ManagementScope(@"\\MyServer\root\cimv2", connectionOptions); 
managementScope.Connect(); 

ManagementPath path = new ManagementPath("Win32_Service"); 
ManagementClass services = new ManagementClass(managementScope, path, null); 

foreach (ManagementObject service in services.GetInstances()) 
{ 
    try 
    { 
     if (service.GetPropertyValue("Name").ToString() == "MyService") 
      if (service.GetPropertyValue("State").ToString().ToLower().Equals("running")) 
      { 
       teServiceStatus.BackColor = Color.LightGreen; 
       teServiceStatus.Text = "Running"; 
       sbStartService.Enabled = false; 
       sbStopService.Enabled = true; 
      } 
      else 
      { 
       teServiceStatus.BackColor = Color.Red; 
       teServiceStatus.Text = "Stopped"; 
       sbStartService.Enabled = true; 
       sbStopService.Enabled = false; 
      } 
    } 
    catch (InvalidOperationException ioe) 
    { 
     if (System.Diagnostics.Debugger.IsAttached) 
      System.Diagnostics.Debugger.Break(); 
     else 
     { 
      teServiceStatus.Text = "Cannot Connect"; 
      teServiceStatus.BackColor = Color.Red; 
     } 
    } 
    catch (Exception ex) 
    { 
     if (System.Diagnostics.Debugger.IsAttached) 
      System.Diagnostics.Debugger.Break(); 
     else 
     { 
      teServiceStatus.Text = "Cannot Connect"; 
      teServiceStatus.BackColor = Color.Red; 
     } 
    } 
} 

這工作讓誰使用該程序可以啓動/停止服務,任何用戶看到它的當前狀態,但是當程序關閉,服務停止(不知道是否應該發生,但它是)。

那麼,如何讓我的程序啓動,停止並讀取使用它的服務狀態?我覺得我很接近,但顯然,錯過了一些東西。

UPDATE 當使用ConnectionOptions路線,似乎只要其中ConnectionOptions(或管理範圍)是形式,關閉,那麼服務器記錄它作爲登錄用戶已「登錄關」,並在當時關閉服務。我不知道爲什麼,但仍然試圖研究它。

+0

是否有任何內容正在寫入服務正在運行的計算機上的事件日誌中?這裏可能會有一個未處理的例外。 – iamkrillin 2012-07-09 17:38:00

+0

我沒有顯示任何異常...它顯示服務已經開始,但它什麼時候停止(通過關閉程序)。 – Robert 2012-07-09 17:47:40

+0

我在系統日誌中看到一個錯誤:「MyService服務意外終止,它已經完成了這一次(s),將在60000毫秒內採取以下糾正措施:重新啓動服務。但我沒有看到爲什麼的具體原因。 – Robert 2012-07-09 17:50:27

回答

0

讓您的正在運行的客戶端代碼模擬NT AUTHORITY\NetworkService帳戶,以便它具有與您的服務相同的權限級別。 The password is null(或者可能是空字符串,請試試我猜)。在WindowsIdentity.Impersonate中演示瞭如何在模仿該用戶的同時運行代碼的文檔。有關Impersonation/Delegation的更多信息。

此外,您可能希望在您的服務中實施handlers for all unhandled exceptions。有可能是你的try/catch塊之外拋出異常,這就是爲什麼你的應用程序正在退出,而你的連接調試器沒有打破你在catch塊中設置的任何斷點。