C#:如何以编程方式检查 Web 服务是否已启动并正在运行? [英] C#: How to programmatically check a web service is up and running?
问题描述
我需要创建一个 C# 应用程序来监视一组 Web 服务是否已启动并正在运行.用户将从下拉列表中选择一个服务名称.程序需要用对应的服务 URL 进行测试,显示服务是否正在运行.最好的方法是什么?我想到的一种方法是测试我们是否能够下载 wsdl.有没有更好的办法?
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?
注意:此应用程序的目的是用户只需要知道服务名称.他不需要记住/存储服务的相应 URL.
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.
我需要这个 C# 应用程序的网站版本和桌面应用程序版本.
I need a website version and a desktop application version of this C# application.
注意:现有服务正在使用 WCF.但将来可能会添加非 WCF 服务.
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.
参考
推荐答案
这不能保证功能,但至少您可以检查到 URL 的连接性:
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));
}
这篇关于C#:如何以编程方式检查 Web 服务是否已启动并正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!