WCF RIA Service错误处理

1、服务器端错误处理:

[EnableClientAccess()]
public class DomainService1 : DomainService
{    
  public IEnumerable<Customer> GetCustomers()    
  {        
    throw new ApplicationException("My exception");    
  }    
  protected override void OnError(DomainServiceErrorInfo errorInfo)    
  {         
    //记录错误
  } }

可以在web.config中设置出现错误时导航到错误页:

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm"/>

2、客户端错误处理:

当SL执行load操作时,会抛出异常,如果没有处理会调用Application_UnhandledException,引起白屏。可以这样处理:

customerDomainContext.Load<Customer>(ds.GetCustomersQuery(),  
loadOperation =>
{
if (loadOperation.HasError)
  {
    MessageBox.Show(loadOperation.Error.Message);
    loadOperation.MarkErrorAsHandled();
  }
}
,null);
原文地址:https://www.cnblogs.com/slmk/p/2818081.html