C#:如何以编程方式检查 Web 服务是否已启动并正在运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12094024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:03:01  来源:igfitidea点击:

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

c#asp.net.netwcfweb-services

提问by LCJ

I need to create an C# application that will monitor whether a set of web services are up and running. User will select a service name from a dropdown. The program need to test with the corresponding service URL and show whether the service is running. What is the best way to do it? One way I am thinking of is to test whether we are able to download the wsdl. IS there a better way?

我需要创建一个 C# 应用程序来监视一组 Web 服务是否已启动并正在运行。用户将从下拉列表中选择一个服务名称。程序需要用对应的服务 URL 进行测试,并显示服务是否正在运行。最好的方法是什么?我想到的一种方法是测试我们是否能够下载 wsdl。有没有更好的办法?

Note: The purpose of this application is that the user need to know only the service name. He need not remember/store the corresponding URL of the service.

注意:此应用程序的目的是用户只需要知道服务名称。他不需要记住/存储服务的相应 URL。

I need a website version and a desktop application version of this C# application.

我需要这个 C# 应用程序的网站版本和桌面应用程序版本。

Note: Existing services are using WCF. But in future a non-WCF service may get added.

注意:现有服务使用 WCF。但将来可能会添加非 WCF 服务。

Note: My program will not be aware of (or not interested in ) operations in the service. So I cannot call a service operation.

注意:我的程序不会知道(或不感兴趣)服务中的操作。所以我不能调用服务操作。

REFERENCE

参考

  1. How to check if a web service is up and running without using ping?
  2. C program-How do I check if a web service is running
  1. 如何在不使用 ping 的情况下检查 Web 服务是否已启动并正在运行?
  2. C 程序-如何检查 Web 服务是否正在运行

采纳答案by paul

this would not guarantee functionality, but at least you could check connectivity to a URL:

这不能保证功能,但至少您可以检查与 URL 的连接:

var url = "http://url.to.che.ck/serviceEndpoint.svc";

try
{
    var myRequest = (HttpWebRequest)WebRequest.Create(url);

    var response = (HttpWebResponse)myRequest.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        //  it's at least in some way responsive
        //  but may be internally broken
        //  as you could find out if you called one of the methods for real
        Debug.Write(string.Format("{0} Available", url));
    }
    else
    {
        //  well, at least it returned...
        Debug.Write(string.Format("{0} Returned, but with status: {1}", 
            url, response.StatusDescription));
    }
}
catch (Exception ex)
{
    //  not available at all, for some reason
    Debug.Write(string.Format("{0} unavailable: {1}", url, ex.Message));
}

回答by Victor

I may be too late with the answer but this approach works for me. I used Socket to check if the process can connect. HttpWebRequest works if you try to check the connection 1-3 times but if you have a process which will run 24hours and from time to time needs to check the webserver availability that will not work anymore because will throw TimeOut Exception.

我的答案可能为时已晚,但这种方法对我有用。我使用 Socket 来检查进程是否可以连接。如果您尝试检查连接 1-3 次,则 HttpWebRequest 可以工作,但是如果您有一个 24 小时运行的进程,并且需要不时检查网络服务器的可用性,该可用性将不再起作用,因为会抛出 TimeOut 异常。

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                var result = socket.BeginConnect("xxx.com", 80, null, null);
                bool success = result.AsyncWaitHandle.WaitOne(3000,false); // test the connection for 3 seconds
                var resturnVal = socket.Connected;
                if (socket.Connected)
                    socket.Disconnect(true);
                socket.Dispose();
                return resturnVal;