2011-04-12 124 views

回答

0

首先,我發現這個問題Web Service For Database/Website Status

你應該試試這個代碼;

System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("myService"); 
return sc.Status 

而你應該檢查這段代碼;

public static string PingHost(string args) 
     { 
      HttpWebResponse res = null; 

      try 
      { 
       // Create a request to the passed URI. 
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(args); 
       req.Credentials = CredentialCache.DefaultNetworkCredentials; 

       // Get the response object. 
       res = (HttpWebResponse)req.GetResponse(); 

       return "Service Up"; 
      } 
      catch (Exception e) 
      { 
       MessageBox.Show("Source : " + e.Source, "Exception Source", MessageBoxButtons.OK); 
       MessageBox.Show("Message : " + e.Message, "Exception Message", MessageBoxButtons.OK); 
       return "Host Unavailable"; 
      } 
     } 

而且你也應該看看;

Using Session State in a Web Service

+0

我試過你的解決方案,但它提供異常服務'myservice'沒有在計算機'computername'上找到。 – smartDev 2011-04-12 07:16:33

0

如果要調用在同步模式下你的web服務,沒有與獲取狀態沒有問題。

如果您在異步模式下調用webservice,則應該設置回調函數並跟蹤返回給該回調函數的結果中的web服務狀態。

1
private void button1_Click(object sender, EventArgs e) 
    { 


     var url = "servicsURL"; 

     try 
     { 
      var myRequest = (HttpWebRequest)WebRequest.Create(url); 
      NetworkCredential networkCredential = new NetworkCredential("UserName", "password","domain"); 
      // Associate the 'NetworkCredential' object with the 'WebRequest' object. 
      myRequest.Credentials = networkCredential; 
      var response = (HttpWebResponse)myRequest.GetResponse(); 
      if (response.StatusCode == HttpStatusCode.OK) 
      { 
       // it's at least in some way responsive 
       // but may be internally broken 
       // as you could find out if you called one of the methods for real 
       MessageBox.Show(string.Format("{0} Available", url)); 
      } 
      else 
      { 
       // well, at least it returned... 
       MessageBox.Show(string.Format("{0} Returned, but with status: {1}", url, response.StatusDescription)); 
      } 
     } 
     catch (Exception ex) 
     { 
      // not available at all, for some reason 
      MessageBox.Show(string.Format("{0} unavailable: {1}", url, ex.Message)); 
     } 

    }