且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#:如何以编程方式检查Web服务的启动和运行?

更新时间:2023-08-31 16:52:16

这并不能保证功能,但至少你可以检查到一个网址:

  VAR URL =htt​​p://url.to.che.ck/serviceEndpoint.svc;

        尝试
        {
            VAR myRequest =(HttpWebRequest的)WebRequest.Create(URL);

            变种响应=(HttpWebResponse)myRequest.GetResponse();

            如果(response.Status code ==的HTTPStatus code.OK)
            {
                //它至少以某种方式响应
                //但可以内部破
                //因为你会发现,如果你叫的方法,真正的
                Debug.Write(的String.Format({0}可用,网址));
            }
            其他
            {
                //好,至少它回来...
                Debug.Write(的String.Format({0}回来了,但状态:{1},网址,response.StatusDescription));
            }
        }
        赶上(例外前)
        {
            //无法为所有,出于某种原因
            Debug.Write(的String.Format({0}无效:{1},网址,ex.Message));
        }
 

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?

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.

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

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

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

this would not guarantee functionality, but at least you could check connectivity to a 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));
        }