我正在用.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 (Object reference was not set to an instance of an object)运行时错误,并且似乎没有被C#代码中的catch子句捕获(而我希望该方法返回一个填充了Error字段的空ReplyObject )。
我已经创建了一个测试C#项目并复制了相同的方法(即,我从.NET平台中调用了相同的web服务),并且我可以确认在这种情况下SoapException被正确捕获。
这种行为是故意的吗?
更新:我的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发布于 2013-01-08 00:02:31
我很羞愧,原因很简单……
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就是导致NullPointerException的null ...
发布于 2013-01-04 23:08:09
(这只是一个猜测,更适合评论,但它相当长)
当ReplyObject类超出作用域时,BFRWebServiceconnector运行时可能正在处理BFRWebServiceconnector COM对象,可能是因为它是类的属性,而不是在方法中创建的?
尝试在GetFromBFR中创建ReplyObject,而不是将其设置为该类的属性。如果从不同的线程调用COM对象,这也可以防止多线程访问出现奇怪的错误。
另外,如果VB程序中有一行代码抛出了错误(在调用GetFromBFR之后),您可以查看变量在VB中是否为Nothing,以尝试缩小问题的范围。
就像我说的,只是猜测。你可以反驳它。:)
https://stackoverflow.com/questions/14156090
复制相似问题