bebojoor
bebojoor

Reputation: 219

How to check if a web service is up and running without using ping?

How can i check if a method in a web service is working fine or not ? I cannot use ping. I still want to check any kind of method being invoked from the web service by the client. I know it is difficult to generalize but there should be some way.

Upvotes: 11

Views: 79206

Answers (8)

Mark Walker
Mark Walker

Reputation: 131

Powershell is by far an easy way to 'ping' a webservice endpoint.

Use the following expression:

Test-NetConnection -Port 4408 -ComputerName 192.168.134.1

Here is a failure response for a port that does not exist or is not listening;

WARNING: TCP connect to 192.168.134.1:4408 failed
ComputerName           : 192.168.134.1
RemoteAddress          : 192.168.134.1
RemotePort             : 4408
InterfaceAlias         : Ethernet0
SourceAddress          : 192.168.134.1
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : False

Here is a success result if the address/port is listening and accessible:

ComputerName     : 192.168.134.1
RemoteAddress    : 192.168.134.1
RemotePort       : 4407
InterfaceAlias   : Ethernet0
SourceAddress    : 192.168.134.1
TcpTestSucceeded : True

Upvotes: 5

CS Smit
CS Smit

Reputation: 41

You can write your self a little tool or windows service or whatever you need then look at these 2 articles:

C#: How to programmatically check a web service is up and running?

check to if web-service is up and running - efficiently

EDIT: This was my implementation in a similar scenario where I need to know if an external service still exists every time before the call is made:

bool IsExternalServiceRunning
    {
        get
        {
            bool isRunning = false;
            try
            {
                var endpoint = new ServiceClient();
                var serviceUri  = endpoint.Endpoint.Address.Uri;
                var request = (HttpWebRequest)WebRequest.Create(serviceUri);
                request.Timeout = 1000000;
                var response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                    isRunning = true;
            }
            #region
            catch (Exception ex)
            {
                // Handle error
            }
            #endregion
            return isRunning;
        }
    }

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

The only way to know if a web service method is working "fine" is to call the method and then to evaluate whether the result is "fine". If you want to keep a record of "fine" vs. time, then you can log the result of the evaluation.

There's no more general way to do this that makes any sense. Consider:

  1. You could have code that creates an HTTP connection to the service endpoint, but success doesn't tell you whether the service will immediately throw an exception as soon as you send it any message.
  2. You could connect and send it an invalid message, but that doesn't tell you much.
  3. You could connect and send it a valid message, then check the result to ensure that it is valid. That will give you a pretty good idea that when a real client calls the service immediately afterwards, the real client should expect a valid result.
  4. Unless the service takes that as an opportunity to crash, just to spite you!

The best technique would be to use WCF tracing (possibly with message-level tracing) to log what actually happens with the service, good or bad. A human can then look at the logs to see if they are "fine".

Upvotes: 4

The Pig's Ear
The Pig's Ear

Reputation: 253

I use this method and it works fine :

public bool IsAddressAvailable(string address)
{
    try
    {
        System.Net.WebClient client = new WebClient();
        client.DownloadData(address);
        return true;
    }
    catch
    {
        return false;
    }
}

Upvotes: 8

Louis Kottmann
Louis Kottmann

Reputation: 16618

As I see it, you have 2 options:

  • If you can access the server it is running on, Log every call (and exceptions thrown).
    Read the log file with a soft like baretail that updates as the file is being written.

  • If you can't access the server, then you have to make the webservice write that log remotely to another computer you have access to.
    Popular loggers have this functionality built in. (Log4Net, ...)

Upvotes: 0

18446744073709551615
18446744073709551615

Reputation: 16832

You may try curl. It's a Linux tool, should be there in Cygwin too.

$ curl http://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

There are lots of options; examples can be found in the 'net.

Upvotes: 1

Rumplin
Rumplin

Reputation: 2768

just use try catch inside the method of your webservice and log exceptions to a log file or to the event log. Example:

[OperationContract]
 public bool isGUID(string input)
{
    bool functionReturnValue = false;

    try
    {
        Guid guid;
        functionReturnValue = Guid.TryParse(input, guid);
    }
    catch (Exception ex)
    {
        Log.WriteServerErrorLog(ex);
    }

    return functionReturnValue;
}

You don't need to ping the webservice, but instead ping the server with a watchdog service or something. There is no need to "ping" the webservice. I also think you don't need to do this anyway. Either your webservice works or it doesn't because of bad code.

Upvotes: 1

Related Questions