4

How can I use the FaultContract attribute with RIA Services so that I'll be able to pass additional information as part of an exception to Silverlight?

sebagomez
  • 9,501
  • 7
  • 51
  • 89
sternr
  • 6,216
  • 9
  • 39
  • 63

1 Answers1

2

So I went hunting though the decompiled RIA Services code. It doesn't seem like it's possible to significantly alter the error info that is sent to the client.

You are able to override the OnError() method in your DomainService, but this doesn't allow you to pass arbitrary information back, even if it's a custom exception type.

The reason is buried inside the exception handling of the DomainServices.Hosting.QueryProcessor class.

If an unhandled exception occurs in a domain operation, it bubbles back and then a FaultException() is ultimately thrown (which WCF natively handles).

Unfortuantely, the DomainServiceFault class is very lightweight... It only has a few properties...

public class DomainServiceFault
{
    public int ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    public bool IsDomainException { get; set; }
    public string StackTrace { get; set; }
    public IEnumerable<ValidationResultInfo> OperationErrors { get; set; }

    public IEnumerable<ValidationResult> GetValidationErrors()
    {}
}

and these are populated in the ServiceUtility.CreateFaultExceotion() like so:

DomainServiceFault detail = new DomainServiceFault();
<snip/>
detail.ErrorCode = domainException.ErrorCode;
detail.ErrorMessage = ServiceUtility.FormatExceptionMessage((Exception) domainException);
detail.IsDomainException = true;

if (current != null && !current.IsCustomErrorEnabled)
    detail.StackTrace = domainException.StackTrace;

return new FaultException<DomainServiceFault>(detail, new FaultReason(new FaultReasonText(detail.ErrorMessage ?? string.Empty, CultureInfo.CurrentCulture)));

It's worth noting in the case of an Exception, rather than validation errors, the OperationErrors are not populated.

So the upshot of all of this is that I don't believe it's possible to wrap or attach custom exception information to the DomainService error handler (which is really unfortunate).

Alastair Pitts
  • 19,423
  • 9
  • 68
  • 97
  • All this small shortcomings make you feel like the RIA Services framework is just not ready for real world scenarios... Thanks anyway! – sternr Mar 14 '12 at 10:30
  • @sternr We often feel that way. I think RIA Services works really well for basic data models & applications. For extremely detailed & complex applications, it starts to struggle. – Alastair Pitts Mar 14 '12 at 22:34