I am creating a small test application that will go through several test cases to check the status of various components of our system. The application is created as a .NET Windows Forms application with several flags that are checked for each test case that passes (and is not checked if it fails).
One test is to check the availability of a simple web service. If the web service is available, the test case should succeed, otherwise it will fail. First, I tried to accomplish this using the System.Net.HttpWebRequest class, setting the timeout to 1500 ms and calling GetResponse. Now, if the web service is unavailable, the request should be timed out, and the test case fails. However, as it turned out, the timeout is set to the actual response of the web service. That is, the timeout starts counting from the moment a connection to the web service is established. If the web service is simply unavailable, a timeout does not occur, and therefore, it will take at least 30 seconds until the test test completes.
To solve this problem, I was asked to make an asynchronous call using BeginGetResponse with a callback when the request is complete. Now the same problem arises here, because if the web service is unavailable, it will take at least 30 seconds until the callback occurs.
Now I thought I could use System.Timers.Timer, set the timeout to 1500 ms and have an event when the timer expires. The problem is that the timer will time out regardless of whether the web service is available, and therefore the event will be fired regardless of whether the web service is available.
Now I have no ideas. I understand that this should be a fairly simple problem to solve. Anyone have any tips on how I could do this?
. , . , , , . , . :
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();
, ? Thanx!