2010-10-22 48 views
3

我有一個基於SoapHttpClientProtocol的WebService引用(.NET CF 3.5)。我的問題是 - 有沒有方法可以確定是否建立了與WebService的連接,而不是調用Web方法?我可以隨時檢查底層連接是否已建立並獲得其狀態?SoapHttpClientProtocol/HttpWebClientProtocol連接狀態

Regards

+0

你需要通過代碼做出這個決定嗎? – 2011-05-13 14:40:36

回答

1

您可以檢查客戶端的通信狀態。

using (XServiceSoapClient client = new XServiceSoapClient()) 
{ 
    client.State; 
} 

public enum CommunicationState 
{ 
    // Summary: 
    //  Indicates that the communication object has been instantiated and is configurable, 
    //  but not yet open or ready for use. 
    Created = 0, 
    // 
    // Summary: 
    //  Indicates that the communication object is being transitioned from the System.ServiceModel.CommunicationState.Created 
    //  state to the System.ServiceModel.CommunicationState.Opened state. 
    Opening = 1, 
    // 
    // Summary: 
    //  Indicates that the communication object is now open and ready to be used. 
    Opened = 2, 
    // 
    // Summary: 
    //  Indicates that the communication object is transitioning to the System.ServiceModel.CommunicationState.Closed 
    //  state. 
    Closing = 3, 
    // 
    // Summary: 
    //  Indicates that the communication object has been closed and is no longer 
    //  usable. 
    Closed = 4, 
    // 
    // Summary: 
    //  Indicates that the communication object has encountered an error or fault 
    //  from which it cannot recover and from which it is no longer usable. 
    Faulted = 5, 
} 
0

我很確定沒有連接,除非你在接口上調用方法。特別是因爲通信是基於HTTP的。

在我正在開發的一個項目中,我實際上在客戶端可以調用的服務器上創建了一個NOP方法。我用它來確定提供的連接信息和登錄憑證是否有效(通過使用try-catch塊)。