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的连接:
  1. var url = "http://url.to.che.ck/serviceEndpoint.svc";
  2.  
  3. try
  4. {
  5. var myRequest = (HttpWebRequest)WebRequest.Create(url);
  6.  
  7. var response = (HttpWebResponse)myRequest.GetResponse();
  8.  
  9. if (response.StatusCode == HttpStatusCode.OK)
  10. {
  11. // it's at least in some way responsive
  12. // but may be internally broken
  13. // as you could find out if you called one of the methods for real
  14. Debug.Write(string.Format("{0} Available",url));
  15. }
  16. else
  17. {
  18. // well,at least it returned...
  19. Debug.Write(string.Format("{0} Returned,but with status: {1}",url,response.StatusDescription));
  20. }
  21. }
  22. catch (Exception ex)
  23. {
  24. // not available at all,for some reason
  25. Debug.Write(string.Format("{0} unavailable: {1}",ex.Message));
  26. }

猜你在找的C#相关文章