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

标签 c# asp.net .net wcf web-services

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

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

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

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

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

引用

  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

最佳答案

这不能保证功能,但至少您可以检查与 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));
}

关于C#:如何以编程方式检查 Web 服务是否已启动并正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094024/

相关文章:

c# - 如何在c#中使用循环调用不同名称的数组

c# - 通过将类名作为字符串提供来获取引用程序集中的类型?

c# - ASP.NET 亚马逊商品搜索

.net - .NET反射问题

c# - Visual Studio 的 Outlook 对象库

.net - Visual Studio 忽略 MSBuild 文件 (csproj) 自定义

c# - String.Split 返回奇怪的结果

c# - .NET 在服务器上创建计划任务失败并显示 E_ACCESSDENIED

c# - 如何通过 jQuery 调整 iFrame 的大小?

c# - 制作自己的 Windows 8 应用程序主题