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

前端之家收集整理的这篇文章主要介绍了C#:如何以编程方式检查Web服务是否正在运行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要创建一个C#应用程序来监视一组Web服务是否启动并运行.用户将从下拉列表中选择一个服务名称.该程序需要使用相应的服务URL进行测试,并显示该服务是否正在运行.最好的方法是什么?我想的一个方法是测试我们是否能够下载wsdl.有没有更好的办法?

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

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

注意:现有服务正在使用WCF.但是将来可能会添加一个非WCF服务.

注意:我的程序不会意识到(或不感兴趣)在服务中的操作.所以我无法调用服务操作.

参考

> How to check if a web service is up and running without using ping?
> C program-How do I check if a web service is running

解决方法

这不能保证功能,但至少您可以检查与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}",ex.Message));
}
原文链接:https://www.f2er.com/csharp/95202.html

猜你在找的C#相关文章