RIA服务和FaultContract [英] RIA Services and FaultContract

查看:201
本文介绍了RIA服务和FaultContract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在RIA Services中使用FaultContract属性,以便我可以传递额外的信息作为Silverlight的一个例外。

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?

推荐答案

所以我通过反编译的RIA服务代码去狩猎。似乎没有可能显着改变发送给客户端的错误信息。

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.

您可以覆盖 OnError( )方法,但这不允许您传递任意信息,即使它是自定义异常类型。

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.

原因被掩埋在 DomainServices.Hosting.QueryProcessor 类的异常处理之内。

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

如果未处理的异常发生在一个域操作,它反弹,然后一个FaultException()最终抛出(WCF本身处理)。

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

不幸的是, DomainServiceFault class非常轻量级...它只有几个属性...

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()
    {}
}

这些填充在 ServiceUtility.CreateFaultExceotion()像这样:

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

值得注意的是在异常情况下,而不是验证错误, OperationErrors 没有填充。

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

所有这一切的结果是我不相信可以包装或附加自定义异常信息到DomainService错误处理程序(这真是不幸)。

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

这篇关于RIA服务和FaultContract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆