我需要创建一个 C# 应用程序来监视一组 Web 服务是否已启动并正在运行。用户将从下拉列表中选择一个服务名称。程序需要用相应的服务URL进行测试,显示服务是否正在运行。最好的方法是什么?我想到的一种方法是测试我们是否能够下载 wsdl。有没有更好的办法?
注意:此应用程序的目的是用户只需要知道服务名称。他不需要记住/存储服务对应的URL。
我需要此 C# 应用程序的网站版本和桌面应用程序版本。
注意:现有服务正在使用 WCF。但将来可能会添加非 WCF 服务。
注意:我的程序不会知道(或不感兴趣)服务中的操作。所以我不能调用服务操作。
引用
最佳答案
这不能保证功能,但至少您可以检查与 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/