如何以编程方式检查wcf服务是否已停止

时间:2012-06-11 08:45:02

标签: c# wcf windows-phone-7

我正在做一个Windows Phone 7应用程序,它使用wcf服务来收集与应用程序相关的数据。如果服务暂时停止,我需要给用户一个消息来尝试。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

你可以通过向它发送http请求来检查服务是否在线吗?

public static Boolean isSiteOnline(String url)
    {
        Boolean result = true;
        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
        httpReq.AllowAutoRedirect = false;
        HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
        if (httpRes.StatusCode != HttpStatusCode.OK)
            result = false;

        httpRes.Close();
        return result;
    }