1

我正在.NET 中开发一个 ComVisible 库,然后在旧的 VB6 类中调用它。我在课堂上主要做的是调用 Web 服务、解析响应并返回包含必要数据的对象。Web 服务被设计为返回一个SoapExceptionif 调用错误的参数。这是我的代码的一部分:

    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 程序中收到-245757( Object reference was not set to an instance of an object) 运行时错误,而且它似乎没有被catch我的 C# 代码中的子句捕获(虽然我希望该方法返回一个ReplyObject带有填充字段的空字段) Error.

我创建了一个测试 C# 项目并复制了相同的方法(即我从 .NET 平台内调用相同的 Web 服务),我可以确认在这种情况下SoapException它被正确捕获。

这种行为是故意的吗?有没有办法SoapException在 ComVisible 类中捕获 (因为我真的想将错误消息包含到我的回复对象中)?

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
4

2 回答 2

0

(这只是一个猜测,更适合评论,但它很长)

当类超出范围时,.NET 运行时可能正在处理ReplyObjectCOM 对象,可能是因为它是类的属性而不是在方法中创建的?BFRWebServiceconnector

尝试创建ReplyObject内部GetFromBFR而不是使其成为类的属性。如果从不同线程调用 COM 对象,这也可以防止多线程访问出现奇怪的错误。

此外,如果 VB 程序中有特定行抛出错误(在您调用 之后GetFromBFR),您可以查看变量是否Nothing在 VB 中以尝试缩小问题范围。

就像我说的,只是一个猜测。随意反驳它。:)

于 2013-01-04T15:08:09.117 回答
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.InnerExceptionnull导致NullPointerException...

于 2013-01-07T16:02:30.677 回答