简体   繁体   English

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

[英]WCF RIA Services Business Rule /Database Exception Handling

I am using WCF RIA Services VS2008/.NET 3.5 and trying to do exception handling. 我正在使用WCF RIA服务VS2008 / .NET 3.5,并尝试执行异常处理。 I have overridden OnError method of DomainService and doing my exception handling in that method. 我已经重写了DomainService的OnError方法,并在该方法中进行了异常处理。 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. 问题是客户端始终会收到DomainServiceException,并且原始错误消息的第一行显示操作名称失败。 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 我尝试将特殊字符串添加到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);
        }
    }

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. 请提出建议。

In the RTM update you'll be able to set the Error property on DomainServiceErrorInfo you want to send back to the client. 在RTM更新中,您将能够在要发送回客户端的DomainServiceErrorInfo上设置Error属性。 At that point, you can set it to an instance of a DomainException. 此时,您可以将其设置为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). 您可以通过从OnError抛出新的DomainException来获得类似的行为,尽管这可能是一个棘手的解决方法(假设它可行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM