title: How to check if a webservice is available using an asynchronous call
body: <p>I'm creating a small test application that will run through a few test cases to check the status of various components of our system. The application is created as a .NET Windows Forms application with a few checkboxes that gets checked in for each test case that passes (and not checked if failed). </p>
  
  <p>One of the tests is to check the availability of a simple webservice. If the webservice is available, the test case is to succeed, otherwise fail. I first tried to accomplish this by using the System.Net.HttpWebRequest class, setting the timeout to 1500 ms and invoking GetResponse. Now, if the webservice is not available the request should timeout and the test case fails. However, as it turns out, the timeout is set on the actual response from the webservice. That is to say, the timeout starts counting from when a connection to the webservice has fist been established. If the webservice is simply unreachable, the timeout does not occur, and consequently it will take at least 30 seconds until the testcase fails.</p>
  
  <p>To solve this problem, I was advised to make an asynchronous call using BeginGetResponse with a callback for when the request is finished. Now, the same problem occurrs here, because if the webservice is not available, it will take at least 30 seconds until the callback occurs.</p>
  
  <p>Now I was thinking that I could use a System.Timers.Timer, set timeout to 1500 ms and have an event for when the timer times out. The problem is, the timer will timeout independently of whether the webservice is accessible and consequently the event will be fired independently of whether the webservice is accessible. </p>
  
  <p>Now I'm out of ideas. I realize that this must be a fairly easy problem to solve. Do anyone have any advice on how I could accomplish this?</p>
  
  <p>I got the advice to make the call in its own thread. So now I'm performing the request in its own thread. However, if the request dont have a response, the application locks when I'm trying to set the test status to failed. But if it has a response I can set the test status to succeeded withouut any problems. The code for this:</p>
  
  <pre><code>private void TestWebServiceConnection()
          {
              HttpWebRequest request;
              request = (HttpWebRequest)WebRequest.Create(Url);
              request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
              System.Threading.Thread.Sleep(1500);
              SetStatusDelegate d = new SetStatusDelegate(SetTestStatus);
              if (request.HaveResponse)
              {
                  if (ConnectionTestCase.InvokeRequired)
                  {
                      this.Invoke(d, new object[] { TestStatus.Success});
                  }
              }
              else
              {
                  if (ConnectionTestCase.InvokeRequired)
                  {
                      this.Invoke(d, new object[] { TestStatus.Failure });
                  }
              }
  
          }
          private delegate void SetStatusDelegate(TestStatus t);
  
          private void SetTestStatus(TestStatus t)
          {
              ConnectionTestCase.Status = t;
          }
  
  ThreadStart ts = new ThreadStart(TestWebServiceConnection);
  Thread t = new Thread(ts);
  t.Start();
  </code></pre>
  
  <p>Any advice on why I get this behaviour? Thanx!</p>