本文是对园子中Artech的WCF异常处理文章的学习笔记:原文网址
http://www.cnblogs.com/artech/archive/2009/10/21/1587716.html
基本异常处理模式:
(1)异常开关:IncludeExceptionDetailInFaults。
使用方式:配置的方式和应用自定义特性(Custom Attribute)的方式。
配置方式:
1: <?xml version="1.0" encoding="utf-8" ?> 2: <configuration> 3: <system.serviceModel> 4: <behaviors> 5: <serviceBehaviors> 6: <behavior name="serviceDebuBehavior"> 7: <serviceDebug includeExceptionDetailInFaults="true" /> 8: </behavior> 9: </serviceBehaviors> 10: </behaviors> 11: <services> 12: <service behaviorConfiguration="serviceDebuBehavior" name="Artech.WcfServices.Services.CalculatorService"> 13: <endpoint address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding" contract="Artech.WcfServices.Contracts.ICalculator" /> 14: </service> 15: </services> 16: </system.serviceModel> 17: </configuration>
自定义特性的
1: using Artech.WcfServices.Contracts; 2: using System.ServiceModel; 3: namespace Artech.WcfServices.Services 4: { 5: [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 6: public class CalculatorService : ICalculator 7: { 8: //省略服务成员 9: } 10: }
FaultException<TDetail>与FaultException的关系
1: [Serializable] 2: public class FaultException<TDetail> : FaultException 3: { 4: // 其他成员 5: public FaultException(TDetail detail); 6: public TDetail Detail { get; } 7: }
System.ServiceModel.ExceptionDetail与System.Exception设计类似
1: [DataContract] 2: public class ExceptionDetail 3: { 4: // 其他成员 5: public ExceptionDetail(Exception exception); 6: 7: [DataMember] 8: public string HelpLink { get; private set; } 9: [DataMember] 10: public ExceptionDetail InnerException { get; private set; } 11: [DataMember] 12: public string Message { get; private set; } 13: [DataMember] 14: public string StackTrace { get; private set; } 15: [DataMember] 16: public string Type { get; private set; } 17: }