Exception handling in RIA Service

北慕城南 提交于 2019-12-05 12:18:31

问题


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


回答1:


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.




回答2:


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/




回答3:


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


来源:https://stackoverflow.com/questions/5766527/exception-handling-in-ria-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!