开发者

Exception handling in RIA Service

As you know, it's recomended handle exceptions using FaultException with standard WCF service to hide exception details. That's fine but i'm having problem with WCF Ria service. I want to throw an exception from domain service and the client will handle that exception. I want to avoid disclosing exception's sensitive information such as stack trace, method names etc. If it were standard WCF service, I'd use FaultException exception, but in Ria service, it's not working. No matter what kind of Exception I throw from domain service, the client always gets DomainOperationException. Is there any way I can throw a FaultException to the silverlight client from domain service (to not disclose actual exception details)? For example, I have a login window. When the user hit's login button, there should be several validation failures, such as:

I want to have fault types for each error that may occure. The client should check what went wrong and display error message accordingly. I disabled customErrors but it didn't help. Any help would be appreciated. Thanks


Here's what Colin Blair answered to my question here

The DomainService has an overridable method named OnError. Whenever there is an exception within the DomainService itself (not within the WCF code) the exception will be passed to OnError before it is rethrown to be sent back to the client. If you replace the exception in the DomainServiceErrorInfo passed into the OnError method with your own exception then your exception will be the one that gets sent back to the client. If you use the DomainException for your exception then you will be able to pass in an ErrorCode integer which you can use client side to determine the actual error.

It answers my question and needs. Thanks Colin.


I've read about using WCF faults in Silverlight, but haven't yet tried it with WCF RIA.

http://mark.mymonster.nl/2011/02/10/make-use-of-wcf-faultcontracts-in-silverlight-clients/


Code example:

[EnableClientAccess()]
public class YourDomainService : DomainService
{
    protected override void OnError(DomainServiceErrorInfo errorInfo)
    {
            base.OnError(errorInfo);

            customErrorHandler(errorInfo.Error);
    }

    private void customErrorHandler(Exception ex)
    {
            DomainServiceContext sc = this.ServiceContext;

            //Write here your custom logic handling exceptions
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜