通过WCF从MS Dynamics CRM插件获取有用的错误消息 [英] Getting Useful Error Messages from MS Dynamics CRM Plugins through WCF

查看:78
本文介绍了通过WCF从MS Dynamics CRM插件获取有用的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a Silverlight 4 front-end with a WCF Service middle tier, which in turn references CRM 4 web services.

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

When a InvalidPluginExecutionException occurs in our plugins, the best error message I can obtain from the WCF FaultException is a general SoapException message, which really isn't that helpful.

是否有一种方法可以从我们的插件中获取特定的错误消息,这些插件一直传播到WCF(最终到我们的Silverlight应用程序).我们想知道哪些插件失败了.

Is there a way to get specific error messages from our plugins propogated up to our WCF (and eventually to our Silverlight app). We want to know which Plugins are failing.

我在这里发布了答案:

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

推荐答案

我们使用了广泛的try catch块.错误消息存储在名为errorMessage的变量中,然后作为记录保存在我们创建的名为错误日志的自定义实体中(我们在finally方法中进行检查,如果不为空,则创建记录). /p>

We use an extensive try catch block. The error message is stored in a variable called errorMessage and then saved as a record in a custom entity called error logs that we created (we do a check on it in the finally method, and if it's not empty we create the record).

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");
        }
    }
}

这篇关于通过WCF从MS Dynamics CRM插件获取有用的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆