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

后端 未结 8 922
小蘑菇
小蘑菇 2020-12-30 09:42

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 c

相关标签:
8条回答
  • 2020-12-30 10:00

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-30 10:01

    You could also use tracing.

    http://msdn.microsoft.com/en-us/library/ms732023.aspx

    http://msdn.microsoft.com/en-us/library/ms733025.aspx

    0 讨论(0)
  • 2020-12-30 10:03

    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
    
    0 讨论(0)
  • 2020-12-30 10:04

    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.

    0 讨论(0)
  • 2020-12-30 10:11

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

    0 讨论(0)
  • 2020-12-30 10:12

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题