简体   繁体   中英

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:

  • Invalid username or password
  • User account is locked
  • The account is not activated
  • etc

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.

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
    }
}

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM