问题
I am trying to find out a way to handle unhandled exceptions at server side. I want control over exception handling at Domain Service level so that when any operation is called then I can log the exception and wrap it in some beautiful words to send it to the client. I found many ways for WCF and classic web services but nothing for RIA services. Please provide your suggestions.
回答1:
You need to override the DomainService.OnError method as described in this WCF RIA Services Exception Handling blog entry. I've done this in a base DomainService class rather than in each individual DomainService class in my application as shown below. The errorInfo.Error property does not have a setter so I don't think you can replace the exception object with your own, but the <system.web><customErrors mode="On" />
config element can prevent the details of the exception being exposed to the client.
[EnableClientAccess()]
public class DomainServiceBase : DomainService
{
protected override void OnError(DomainServiceErrorInfo errorInfo)
{
Logger.WriteException(errorInfo.Error);
base.OnError(errorInfo);
}
}
回答2:
You can look for specific Errors and then throw a DomainException, which is different than the standard DomainOperationException. The DomainException has an ErrorCode in it, that can be used on the client to specify a condition that will be dealt with differently on the client.
See Kyle McClellan's response at http://forums.silverlight.net/forums/t/193028.aspx. Kyle's blog is a very good resource for these types of questions as well.
来源:https://stackoverflow.com/questions/1897188/ria-services-server-side-exception-handling