WCF RIA 服务业务规则/数据库异常处理

发布于 2024-08-16 14:17:17 字数 1521 浏览 1 评论 0原文

我正在使用 WCF RIA Services VS2008/.NET 3.5 并尝试进行异常处理。我已经重写了 DomainService 的 OnError 方法,并在该方法中进行异常处理。我试图以某种形式向客户端抛出业务规则异常或数据库异常,以便客户端识别它们并以不同的方式处理它们。 问题是客户端总是收到 DomainServiceException 和原始错误消息,第一行显示操作名称失败。因此,我无法识别客户端的异常类型。我尝试向 OnError 中的某些类型的异常添加特殊字符串,如下所示,

   /// <summary>
    /// Exception handling and logging on error
    /// </summary>
    /// <param name="errorInfo"></param>
    protected override void OnError(DomainServiceErrorInfo errorInfo)
    {
        Exception exceptionToLog = null;
        //if exception is business rule exception then log only if there's an inner exception
        if (errorInfo.Error.GetType() == typeof(BusinessRuleException))
        {
            if (errorInfo.Error.InnerException != null)
            {
                exceptionToLog = errorInfo.Error;
            }
            //send the business rule exception to client
            base.OnError(new DomainServiceErrorInfo(new DomainException("BRE:" + errorInfo.Error.Message)));
        }
        else
        {
            exceptionToLog = errorInfo.Error;
            //if its some other server error then send only generic message.
            base.OnError(new DomainServiceErrorInfo(new DomainException(ValidationErrorResources.MSG_GenericServerError)));
        }

        if (exceptionToLog != null)
        {
            //log exception
            EntLibHelper.LogError(exceptionToLog);
        }
    }

但这个技巧似乎不起作用。 有什么方法可以将一些额外的信息附加到我从服务器抛出到客户端的异常中。 请建议。

I am using WCF RIA Services VS2008/.NET 3.5 and trying to do exception handling. I have overridden OnError method of DomainService and doing my exception handling in that method. I am trying to throw Business Rule Exceptions or Database Exceptions to client in some form so that client recognizes them and handles them differently.
The problem is that client always receives DomainServiceException and the original error message with First line showing the operation name failed. So, there's no way I can identify the exception type on client side. I tried adding a special string to certain kind of exceptions in OnError as below

   /// <summary>
    /// Exception handling and logging on error
    /// </summary>
    /// <param name="errorInfo"></param>
    protected override void OnError(DomainServiceErrorInfo errorInfo)
    {
        Exception exceptionToLog = null;
        //if exception is business rule exception then log only if there's an inner exception
        if (errorInfo.Error.GetType() == typeof(BusinessRuleException))
        {
            if (errorInfo.Error.InnerException != null)
            {
                exceptionToLog = errorInfo.Error;
            }
            //send the business rule exception to client
            base.OnError(new DomainServiceErrorInfo(new DomainException("BRE:" + errorInfo.Error.Message)));
        }
        else
        {
            exceptionToLog = errorInfo.Error;
            //if its some other server error then send only generic message.
            base.OnError(new DomainServiceErrorInfo(new DomainException(ValidationErrorResources.MSG_GenericServerError)));
        }

        if (exceptionToLog != null)
        {
            //log exception
            EntLibHelper.LogError(exceptionToLog);
        }
    }

But this trick does not seem to be working.
Is there any way I can attach some extra information, to the exception I am throwing from the server to the client.
Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-08-23 14:17:17

在 RTM 更新中,您将能够在要发送回客户端的 DomainServiceErrorInfo 上设置 Error 属性。此时,您可以将其设置为 DomainException 的实例。

您可以通过从 OnError 中抛出一个新的 DomainException 来获得类似的行为,尽管这将是一个 hacky 解决方法(假设它有效)。

In the RTM update you'll be able to set the Error property on DomainServiceErrorInfo you want to send back to the client. At that point, you can set it to an instance of a DomainException.

You might be able to get similar behavior by throwing a new DomainException from your OnError, though that would be a hacky workaround (assuming it works).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文