检查Web服务连接

时间:2016-02-25 13:04:37

标签: c# web-services windows-phone-8.1

我开发了一个简单的Web服务和一个Windows Phone 8应用程序来使用它。 一切都按预期工作,但我当前(工作)代码的方式,它假设Web服务始终运行和可用。看起来情况可能并非总是如此,我想在开始发送请求之前尝试添加某种连接测试。我已经读过,除了以某种方式查询之外,没有直接确定WS启动和运行的方法。

考虑到这一点,这就是我的 LoadData 方法结构( 2月26日):

public void LoadData(string articleCode = null)
{
    try
    {
        this.ArticleItems.Clear();
        MyServiceSoapClient ws = new MyServiceSoapClient();
        CheckWebService(); 
        if (this.isWebServiceUp)
        {
            if (!String.IsNullOrEmpty(articleCode))
            {
                ws.GetBasicDataAsync(articleCode);
                ws.GetBasicDataCompleted += Ws_GetBasicDataCompleted;
                //(irrelevant code supressed for clarity)

                this.IsDataLoaded = true;                   
            }
        }
        else
        {
            this.ArticleItems.Add(new ItemViewModel() { LineOne = "Could not connect to Web Service." });
            ws.Abort();
        }
    }
}

它仍然会引发unhandled CommunicationException错误。

编辑:在考虑了这些建议并进行了一些搜索之后,我尝试设置了一个“心跳”类型的方法,但它无法正常工作。异步编程对我来说是一个相对较新的范例,所以我很可能错过了一些东西,但是到目前为止我尝试了一个实现(得到一个“无法将'System.Net.Browser.OHWRAsyncResult'类型的对象转换为类型'System.Net.HttpWebResponse'“例外):

public void CheckWebService()
{ 
    try
    {
        Uri wsURL = new Uri("http://localhost:60621/WebService1.asmx");
        //try accessing the web service directly via its URL
        var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(wsURL);
        request.Method = "HEAD";
        //next line throws: "Unable to cast object of type 'System.Net.Browser.OHWRAsyncResult' to type 'System.Net.HttpWebResponse'."
        using (var response = (System.Net.HttpWebResponse)request.BeginGetResponse(new AsyncCallback(ServiceCallback), request))
        {
            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception("Error locating web service");
            }
        }
    }
    catch (System.ServiceModel.FaultException fe)
    {
        this.ArticleItems.Add(new ItemViewModel() { LineOne = fe.Message });
    }
    catch (System.ServiceModel.CommunicationException ce)
    {
        this.ArticleItems.Add(new ItemViewModel() { LineOne = ce.Message });
    }
    catch (System.Net.WebException we)
    {
        this.ArticleItems.Add(new ItemViewModel() { LineOne = we.Message });
    }
    catch (Exception ex)
    {
        this.ArticleItems.Add(new ItemViewModel() { LineOne = ex.Message });
    }
}

private void ServiceCallback(IAsyncResult asyncResult)
{
    try
    {
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)asyncResult.AsyncState;
        using (var response = (System.Net.HttpWebResponse)request.EndGetResponse(asyncResult))
        {
            if (response != null && response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                this.isWebServiceUp = true;
                request.Abort();
            }
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

0 个答案:

没有答案