开发者

RIA Services and FaultContract

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜