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