簡體   English   中英

SoapException未在ComVisible類中捕獲

[英]SoapException not caught in a ComVisible class

我正在開發.NET中的ComVisible庫,然后在舊的VB6類中調用它。 我在該類中所做的基本上是調用Web服務,解析響應並返回帶有必要數據的對象。 Web服務的設計使其在使用錯誤的參數調用時返回SoapException 這是我的代碼的一部分:

    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代碼中調用此方法,我將得到正確的答復。 但是,如果我用錯誤的參數調用它, -245757在我的VB6程序中收到-245757Object reference was not set to an instance of an object )運行時錯誤,並且看來C#代碼中的catch子句未捕獲該錯誤(而我希望該方法返回一個空的ReplyObject並填充Error字段)。

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

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

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

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

BFRWebServiceconnector類超出范圍時,.NET運行時可能會處理ReplyObject COM對象,也許是因為它是該類的屬性,而不是在方法內創建的?

嘗試創建ReplyObjectGetFromBFR ,而不是使之類的屬性。 如果從不同的線程調用COM對象,這也可以防止來自多線程訪問的奇怪錯誤。

另外,如果VB程序中有特定行GetFromBFR錯誤(在調用GetFromBFR ),則可以查看變量在VB中是否為Nothing ,以嘗試縮小問題的范圍。

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

我很as愧原因很簡單...而不是:

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.InnerExceptionnull導致了NullPointerException ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM