10

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
bebojoor
  • 209
  • 1
  • 2
  • 9
  • it's actually wierd - you want to check service state, but don't want to ping it.. means dont want to call it. – Alexander Beletsky Apr 18 '12 at 10:00
  • @alexanderb hmm... ping doesnt work here thats y i need an alternative :) and any other method to invoke and check this web service method would be appreciated. – bebojoor Apr 18 '12 at 10:10
  • 1
    @bebojoor there is a similar question to this subject here: http://stackoverflow.com/questions/10088950/check-if-a-webservice-exists/10089249#10089249 I recommend you periodically call an innocuous service method to verify the web service is 'up' – Nick Ryan Apr 18 '12 at 11:31
  • 1
    Possible duplicate of [C#: How to programmatically check a web service is up and running?](http://stackoverflow.com/questions/12094024/c-how-to-programmatically-check-a-web-service-is-up-and-running) – bummi Mar 14 '16 at 08:03

8 Answers8

8

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;
    }
}
The Pig's Ear
  • 233
  • 1
  • 3
  • 14
4

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".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
3

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
Mark Walker
  • 111
  • 1
  • 4
1

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.

Rumplin
  • 2,703
  • 21
  • 45
  • While logging is generally a good idea, logging only exception doesn't help: _I still want to check any kind of method being invoked from the web service by the client_. Hopefully he doesn't throw an exception at every method of his webservice ;) – Louis Kottmann Apr 18 '12 at 09:58
  • if he can write an exception log, he can write a log insite the try brackets for stats too, I see no problem there. – Rumplin Apr 18 '12 at 10:03
1

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.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • hmmm? Is your code checking any web service method at all? I dont want to use cURL... and i am using windows platform :) – bebojoor Apr 18 '12 at 10:12
1

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;
        }
    }
Community
  • 1
  • 1
CS Smit
  • 41
  • 6
0

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, ...)

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88