[英]Is it possible to hide a class parent when exported to COM with ComVisible?
[英]SoapException not caught in a ComVisible class
我正在開發.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
在我的VB6程序中收到-245757
( Object reference was not set to an instance of an object
)運行時錯誤,並且看來C#代碼中的catch
子句未捕獲該錯誤(而我希望該方法返回一個空的ReplyObject
並填充Error
字段)。
我創建了一個測試C#項目,並復制了相同的方法(即,我從.NET平台內調用相同的Web服務),並且可以確認在這種情況下, SoapException
正確捕獲SoapException
。
這種行為是故意的嗎? 有沒有辦法在ComVisible類中捕獲SoapException
(因為我真的很想將錯誤消息包含在我的Reply對象中)?
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
(這只是一個猜測,更適合發表評論,但是很長)
當BFRWebServiceconnector
類超出范圍時,.NET運行時可能會處理ReplyObject
COM對象,也許是因為它是該類的屬性,而不是在方法內創建的?
嘗試創建ReplyObject
內GetFromBFR
,而不是使之類的屬性。 如果從不同的線程調用COM對象,這也可以防止來自多線程訪問的奇怪錯誤。
另外,如果VB程序中有特定行GetFromBFR
錯誤(在調用GetFromBFR
),則可以查看變量在VB中是否為Nothing
,以嘗試縮小問題的范圍。
就像我說的,只是一個猜測。 隨意反駁。 :)
我很as愧原因很簡單...而不是:
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
為null
導致了NullPointerException
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.