2012-04-04 79 views
1

我們擁有一個帶有WCF服務中間層的Silverlight 4前端,後者又引用了CRM 4 Web服務。通過WCF從MS Dynamics CRM插件獲取有用的錯誤消息

當我們的插件中發生InvalidPluginExecutionException時,我可以從WCF FaultException獲得的最佳錯誤消息是一般的SoapException消息,這實際上並沒有那麼有用。

有沒有辦法從我們的插件中獲取特定的錯誤消息,這些消息一直傳播到我們的WCF(最終到我們的Silverlight應用程序)。我們想知道哪些插件失敗。


我張貼我的答案在這裏:

http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/e8ea5060-74dc-4ae6-9a3b-3c824d6dfb1b

+0

經過這一天的工作,基本上我試圖做的是從InvalidPluginExecutionException獲取自定義消息傳播到我的WCF錯誤處理。再次,我可以在WCF服務中得到一個SoapException錯誤,但是無法想出從CRM獲取自定義消息的方法。 有人嗎? 在此先感謝。 – Pliskin 2012-04-04 18:05:01

回答

0

如果我理解正確的話,你可以使用的try-catch這樣在烏拉圭回合插件:

try{ 
... 
}  
catch (Exception e) 
{ 
    throw new InvalidPluginExecutionException("Error in MyCustomPlugin:" + e.Message); 
} 
0

我們使用廣泛的嘗試catch塊。錯誤消息存儲在名爲errorMessage的變量中,然後作爲記錄保存在我們創建的稱爲錯誤日誌的自定義實體中(我們在finally方法中檢查它,如果它不爲空,我們創建記錄)。

try 
{ 
    //invoke the web service here 
} 
catch (System.Net.WebException ex) 
{ 
    tracingService.Trace("WebException"); 
    if (errorMessage != null && errorMessage.Length > 0) 
     errorMessage += "The application terminated with an error.<br />"; 
    else 
     errorMessage = "The application terminated with an error.<br />"; 
    if (ex.Status == WebExceptionStatus.ProtocolError) 
    { 
     errorMessage += "Error Code: " + ((HttpWebResponse)ex.Response).StatusCode; 
     if (ex.Response != null) 
     { 
      using (var errorResponse = (HttpWebResponse)ex.Response) 
      { 
       using (var reader = new StreamReader(errorResponse.GetResponseStream())) 
       { 
        string error = reader.ReadToEnd(); 
        errorMessage = "Error Message (JSON Format): " + error + "<br />"; 
       } 
      } 
     } 
    } 
} 
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) 
{ 
    if (errorMessage != null && errorMessage.Length > 0) 
     errorMessage += "The application terminated with an error.<br />"; 
    else 
     errorMessage = "The application terminated with an error.<br />"; 
    errorMessage += "Timestamp: " + ex.Detail.Timestamp + ".<br />"; 
    errorMessage += "Code: " + ex.Detail.ErrorCode + ".<br />"; 
    errorMessage += "Message: " + ex.Detail.Message + ".<br />"; 
    errorMessage += "Inner Fault: " + 
     (null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault") + ".<br />"; 

    tracingService.Trace("The application terminated with an error."); 
    tracingService.Trace("Timestamp: {0}", ex.Detail.Timestamp); 
    tracingService.Trace("Code: {0}", ex.Detail.ErrorCode); 
    tracingService.Trace("Message: {0}", ex.Detail.Message); 
    tracingService.Trace("Inner Fault: {0}", 
     null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); 
} 
catch (System.TimeoutException ex) 
{ 

    if (errorMessage != null && errorMessage.Length > 0) 
     errorMessage += "The application terminated with an error.<br />"; 
    else 
     errorMessage = "The application terminated with an error.<br />"; 
    errorMessage += String.Format("Message: {0} <br />", ex.Message); 
    errorMessage += String.Format("Stack Trace: {0} <br />", ex.StackTrace); 
    errorMessage += String.Format("Inner Fault: {0} <br />", 
     null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message); 


    tracingService.Trace("The application terminated with an error."); 
    tracingService.Trace("Message: {0}", ex.Message); 
    tracingService.Trace("Stack Trace: {0}", ex.StackTrace); 
    tracingService.Trace("Inner Fault: {0}", 
     null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message); 
} 
catch (System.Exception ex) 
{ 
    tracingService.Trace("The application terminated with an error."); 
    tracingService.Trace(ex.Message); 

    errorMessage = String.Format("The application terminated with an error.<br />"); 
    errorMessage += String.Format(ex.Message + "<br />"); 

    // Display the details of the inner exception. 
    if (ex.InnerException != null) 
    { 
     tracingService.Trace(ex.InnerException.Message); 

     FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException 
      as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>; 
     if (fe != null) 
     { 
      tracingService.Trace("Timestamp: {0}", fe.Detail.Timestamp); 
      tracingService.Trace("Code: {0}", fe.Detail.ErrorCode); 
      tracingService.Trace("Message: {0}", fe.Detail.Message); 
      tracingService.Trace("Trace: {0}", fe.Detail.TraceText); 
      tracingService.Trace("Inner Fault: {0}", 
       null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); 

      errorMessage += String.Format("Timestamp: {0} <br />", fe.Detail.Timestamp); 
      errorMessage += String.Format("Code: {0} <br />", fe.Detail.ErrorCode); 
      errorMessage += String.Format("Message: {0} <br />", fe.Detail.Message); 
      errorMessage += String.Format("Trace: {0} <br />", fe.Detail.TraceText); 
      errorMessage += String.Format("Inner Fault: {0} <br />", 
       null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); 
     } 
    } 
} 
相關問題