views:

730

answers:

1

Hi

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.

A: 

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).

NikhilK
This is a disappointment though for users who can not upgrade to .Net 4.0 in near future.
Gaurave Sehgal