2017-01-27 137 views
0

我有一個自我託管WCF服務的C#應用​​程序。我想在應用程序中添加一個按鈕點擊事件,讓用戶知道服務是否正在運行/託管。有沒有辦法檢測服務是否正在運行/託管?如何判斷WCF服務是否在主機上運行?

如果有人想看看吧,這裏是我使用啓動託管服務的代碼:

 private static void RunService() 
    { 
     System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(AccountingOperationsService.AccountingOperationsService)); 
     System.ServiceModel.Description.ServiceDebugBehavior debug = host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceDebugBehavior>(); 
     // if not found - add behavior with setting turned on 
     if (debug == null) 
     { 
      host.Description.Behaviors.Add(
       new System.ServiceModel.Description.ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true }); 
     } 
     else 
     { 
      // make sure setting is turned ON 
      if (!debug.IncludeExceptionDetailInFaults) 
      { 
       debug.IncludeExceptionDetailInFaults = true; 
      } 
     } 
     try 
     { 
      host.Open(); 


     } 
     catch (Exception ex) 
     { 

      string errorMessage = ex.Message + Environment.NewLine; 
      errorMessage += ex.StackTrace + Environment.NewLine; 

      DevExpress.XtraEditors.XtraMessageBox.Show(errorMessage, "Error Starting Service", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 

回答

4

也許,你需要創建WCF服務方法Ping

public bool Ping() 
{ 
    return true; 
} 

並在應用程序調用Ping

bool itsWork; 
try 
{ 
    itsWork = service.Ping(); 
} 
catch(Exception ex){} 
相關問題