首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ComVisible类中未捕获到SoapException

在ComVisible类中未捕获到SoapException
EN

Stack Overflow用户
提问于 2013-01-04 19:15:15
回答 2查看 322关注 0票数 1

我正在用.NET开发一个ComVisible库,然后在一个旧的VB6类中调用它。我在这个类中所做的基本上就是调用一个web服务,解析响应,并返回一个包含必要数据的对象。web服务的设计使其在使用错误的参数调用时返回SoapException。下面是我的代码的一部分:

代码语言:javascript
复制
    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 (Object reference was not set to an instance of an object)运行时错误,并且似乎没有被C#代码中的catch子句捕获(而我希望该方法返回一个填充了Error字段的空ReplyObject )。

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

这种行为是故意的吗?

更新:我的VB6代码如下:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-08 00:02:31

我很羞愧,原因很简单……

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

我实际上有以下代码:

代码语言:javascript
复制
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就是导致NullPointerExceptionnull ...

票数 0
EN

Stack Overflow用户

发布于 2013-01-04 23:08:09

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

ReplyObject类超出作用域时,BFRWebServiceconnector运行时可能正在处理BFRWebServiceconnector COM对象,可能是因为它是类的属性,而不是在方法中创建的?

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

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

就像我说的,只是猜测。你可以反驳它。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14156090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档