2013-01-04 20 views
1

我正在開發.NET中的ComVisible庫,然後在舊的VB6類中調用該庫。我在課堂上基本上做的是調用Web服務,解析響應並返回一個包含必要數據的對象。如果使用錯誤的參數調用Web服務,則返回SoapException。這裏是我的代碼的一部分:SoapException未在ComVisible類中捕獲

private static WCFPersonClient _client; 
    private static ReplyObject _reply; 

    public BFRWebServiceconnector() 
    { 
     _client = new WCFPersonClient("WSHttpBinding_IWCFPerson"); 
     _reply = new ReplyObject();    
    } 

    [ComVisible(true)] 
    public ReplyObject GetFromBFR(string bestallningsID, string personnr, bool reservNummer = false) 
    { 
     try 
     { 
      var response = new XmlDocument(); 

      //the service operation returns XML but the method in the generated service reference returns a string for some reason    
      var responseStr = _client.GetUserData(orderID, personnr, 3); reason. 

      response.LoadXml(responseStr); 
      //parse the response and fill the reply object 
      ....... 
     } 
     catch (Exception ex) 
     { 
      _reply.Error = "Error: " + ex.Message; 
      if (_client.InnerChannel.State == CommunicationState.Faulted) _client = new WCFPersonClient("WSHttpBinding_IWCFPerson"); //recreate the failed channel 
     } 
     return _reply; 
    } 

有一次,我嘗試從與正確的參數我VB6代碼中調用這個方法,我得到一個正確的答覆。但是如果我用錯誤的參數調用它,我的VB6程序中會出現一個-245757Object reference was not set to an instance of an object)運行時錯誤,並且它似乎沒有被我的C#代碼中的catch子句捕獲(雖然我期望空的ReplyObject填充了Error字段由該方法返回)。

我創建了一個測試C#項目並複製了相同的方法(即我從.NET平臺調用同一個Web服務),並且我可以確認在這種情況下SoapException正在被正確捕獲。

這種行爲是故意的嗎?有沒有辦法在ComVisible類中捕獲SoapException(因爲我真的想將錯誤消息包含到我的回覆對象中)?

UPD:我的VB6代碼如下:

Set BFRWSCReply = New ReplyObject 
Set BFRWSC = New BFRWebbServiceconnector 
Set BFRWSCReply = BFRWSC.GetFromBFR(m_BeställningsID, personnr) 

If Not IsNull(BFRWSCReply) Then 
    If BFRWSCReply.Error= "" Then 
     m_sEfternamn = BFRWSCReply.Efternamn 
     //etc i.e. copy fields from the ReplyObject 
    Else 
     MsgBox BFRWSCReply.Error, vbExclamation 
    End If 
End If 
+0

在catch子句中設置錯誤時,_replay對象未初始化:_reply.Error =「Error:」+ ex.Message;' – Algirdas

+0

@algirdas,不,它在構造函數中被初始化。 – Azimuth

+0

什麼是VB的運行時錯誤? –

回答

0

我很慚愧,原因很簡單......而是以下內容:

catch (Exception ex) 
    { 
     _reply.Error = "Error: " + ex.Message; 
     if (_client.InnerChannel.State == CommunicationState.Faulted) _client = new WCFPersonClient("WSHttpBinding_IWCFPerson"); //recreate the failed channel 
    } 

我其實下面的代碼:

catch (Exception ex) 
    { 
     _reply.Error = "Error: " + ex.Message + "; " + ex.InnerException.Message; 
     if (_client.InnerChannel.State == CommunicationState.Faulted) _client = new WCFPersonClient("WSHttpBinding_IWCFPerson"); //recreate the failed channel 
    } 

和事實證明, ex.InnerException was null which which the NullPointerException ...

0

(這只是一個猜測,是一個評論更合適,但它很長)

這有可能是在.NET運行時配置當BFRWebServiceconnector類超出範圍時,可能是因爲它是類的屬性而不是在方法中創建的,所以ReplyObject COM對象的值是?

嘗試在GetFromBFR範圍內創建ReplyObject,而不是將其設置爲該類的屬性。如果COM對象是從不同的線程調用的,那麼這也可以防止多線程訪問中的奇怪錯誤。

此外,如果在VB程序中有一條拋出錯誤的特定行(在調用GetFromBFR之後),您可以看到VB中的變量是否爲Nothing以嘗試縮小問題的範圍。

就像我說的,只是一個猜測。隨意反駁它。 :)

+0

我會嘗試在方法內創建'ReplyObject'。不知何故,我自己錯過了這一點... – Azimuth

+0

我沒有幫助。我仍然得到同樣的錯誤。 – Azimuth