【问题标题】:Getting Useful Error Messages from MS Dynamics CRM Plugins through WCF通过 WCF 从 MS Dynamics CRM 插件获取有用的错误消息
【发布时间】:2012-04-04 14:25:35
【问题描述】:

我们有一个带有 WCF 服务中间层的 Silverlight 4 前端,该中间层又引用 CRM 4 Web 服务。

当我们的插件中发生 InvalidPluginExecutionException 时,我可以从 WCF FaultException 获得的最佳错误消息是一般的 SoapException 消息,这真的没有多大帮助。

有没有办法从我们的插件中获取特定的错误消息,这些消息传播到我们的 WCF(并最终传播到我们的 Silverlight 应用程序)。我们想知道哪些插件失败了。


我在这里发布了我的答案:

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

【问题讨论】:

  • 经过一天的工作后,基本上我要做的是从 InvalidPluginExecutionException 中获取自定义消息,并将其传播到我的 WCF 错误处理中。同样,我可以在 WCF 服务中收到 SoapException 错误,但无法找到从 CRM 获取自定义消息的方法。有人吗?提前致谢。

标签: wcf silverlight-4.0 crm microsoft-dynamics


【解决方案1】:

如果我理解正确,你可以在你的插件中使用 Try-catch:

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

【讨论】:

    【解决方案2】:

    我们使用广泛的 try 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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2013-03-08
      • 1970-01-01
      • 2018-04-11
      • 2016-03-11
      • 2012-05-21
      相关资源
      最近更新 更多