1

i'm doing following,

 var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
 var response = (HttpWebResponse)request.GetResponse();
 if(response.StatusCode == HttpStatusCode.OK)
 {
  //perform some operations.
 }

in order to check if the service in up and running in my asp.net c# 4.0 application. its giving me expected results but if the service is not running it is taking too much time (nearly about 1 min) to get the response.

is their any other better way to check if the service is up and running?

user3085995
  • 433
  • 1
  • 6
  • 19
  • Or see the [`HttpWebRequest.Timeout` property](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx). – CodeCaster Feb 12 '14 at 11:12

2 Answers2

0

Your request time out property is not letting the fault response to be delievered in a short time. Reffer to this link, here the time out scenario are disscussed very well also visit the link provided in the answer. You should consider using Asynchronous calls to web service (look for Asynchronous calls to a Web service here).

Community
  • 1
  • 1
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
0

You don't have to do much more than you are already doing, other than add a timeout to your request object. The request timeout should be specified in milliseconds, so 5000 for a 5 second timeout.

Be aware you will then need to catch the exception to detect the timeout, this is a System.Net.WebException with the message The operation has timed out, so your code will look something like:

var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
request.Timeout = 5000;
try
{
    var response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
        //perform some operations.
    }
}
catch (WebException wex)
{
    if (wex.Message == "The operation has timed out")
    {
        //   do stuff
    }
}
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • :thanks for the reply.but what if the service is running and taking suppose @ 10 secs for response – user3085995 Feb 12 '14 at 12:54
  • If you "expect" a return of 10 seconds, and that is ok, then use a timeout of 11 seconds. If, however, you want to time how long it takes to return, then use a long timeout (60 seconds) and then use something like `Stopwatch` to time how long it takes to return. – Andy Brown Feb 12 '14 at 12:56
  • my current condition is, my service is not up and running and still its taking time @ 1 min to get the response.how can i get the instant response in this case(if service is not running). – user3085995 Feb 12 '14 at 13:06
  • That's how HttpWebRequest works, it will wait for the `Timeout` period to tell you it is offline. So, you need to define your criteria: e.g. "I need to know within 1 second if there is no response, and if that happens then I want to try for another 10 seconds to see if it is is online but just slow". If this is insufficient tnen you need more advanced monitoring tools on your servers that watch the application pools as well as your external monitoring server. – Andy Brown Feb 12 '14 at 13:08