C#:如何以编程方式检查Web服务的启动和运行? [英] C#: How to programmatically check a web service is up and running?
问题描述
我需要创建一个C#应用程序,将监视一组Web服务是否正常运行。用户会从下拉列表中选择一个服务名称。该项目需要测试与相应的服务URL,并显示该服务是否正在运行。什么是做到这一点的最好方法是什么?我想到的一个方法是检验我们是否能够下载WSDL。有没有更好的办法?
请注意:本应用程序的目的在于,用户只需要知道的服务名。他不一定记得/存储服务的相应的URL。
我需要一个网站版本,此C#应用程序的桌面应用程序版本。
注:现有的服务使用WCF。但是,在未来的一个非WCF服务可能会增加。
注:我的计划不会知道(或不感兴趣)业务服务。所以我不能调用服务操作。
参考
- <一个href="http://stackoverflow.com/questions/10206951/how-to-check-if-a-web-service-is-up-and-running-without-using-ping">How检查Web服务是建立和运行,而不使用ping?
- <一个href="http://stackoverflow.com/questions/6077392/c-program-how-do-i-check-if-a-web-service-is-running">C程序 - 如何检查,如果一个Web服务运行
这并不能保证功能,但至少你可以检查到一个网址:
VAR URL =http://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
- 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
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屋!