我正在開發.NET中的ComVisible庫,然後在舊的VB6類中調用該庫。我在課堂上基本上做的是調用Web服務,解析響應並返回一個包含必要數據的對象。如果使用錯誤的參數調用Web服務,則返回SoapException
。這裏是我的代碼的一部分:SoapException未在ComVisible類中捕獲
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
)運行時錯誤,並且它似乎沒有被我的C#代碼中的catch
子句捕獲(雖然我期望空的ReplyObject
填充了Error
字段由該方法返回)。
我創建了一個測試C#項目並複製了相同的方法(即我從.NET平臺調用同一個Web服務),並且我可以確認在這種情況下SoapException
正在被正確捕獲。
這種行爲是故意的嗎?有沒有辦法在ComVisible類中捕獲SoapException
(因爲我真的想將錯誤消息包含到我的回覆對象中)?
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
在catch子句中設置錯誤時,_replay對象未初始化:_reply.Error =「Error:」+ ex.Message;' – Algirdas
@algirdas,不,它在構造函數中被初始化。 – Azimuth
什麼是VB的運行時錯誤? –