2017-01-27 206 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){} 
相关问题