错误契约[FaultContract]

服务端:

    [ServiceContract]
    interface ITTest 

    {
         [OperationContract]
        [FaultContract(typeof(DivideByZeroException))]
        [FaultContract(typeof(ExceptionDetail))]
        [FaultContract(typeof(ErrorInfo))]
        double Divide(double Number1, double number2);
    }

 

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
    class TTest : ITTest
    {
         public double Divide(double number1, double number2)
        {
            if (number2 == 0)
            {
                //DivideByZeroException dz = new DivideByZeroException("not is 0");
                //throw new FaultException<DivideByZeroException>(dz, dz.Message);

                ErrorInfo calException = new ErrorInfo();
                calException.ErrorCode = "8888888";
                calException.ErrorMessage = "Divide zero exception";
                FaultException<ErrorInfo> fault = new FaultException<ErrorInfo>(calException, "error");
                throw fault;
            }

            try
            {
                File.Open(@"m:\aaa.txt", FileMode.Open);
            }
            catch
            {
                ExceptionDetail ed = new ExceptionDetail(new Exception("无此文件"));
                throw new FaultException<ExceptionDetail>(ed, "not found file");
                //throw new FaultException<ErrorInfo>();
            }
            return number1 / number2;
        }
    }

 

自定义错误契约:

    public class ErrorInfo
    {
        private string errorCode = string.Empty;
        [DataMember]
        public string ErrorCode
        {
            get { return errorCode; }
            set { errorCode = value; }
        }

        private string errorMessage = string.Empty;
        [DataMember]
        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }
    }

 

客户端:

 ITTest HttpProxy = ChannelFactory<ITTest>.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8001"));

            using (HttpProxy as IDisposable)
            {
                 try
                {
                    Console.WriteLine(HttpProxy.Divide(8, 0));
                }

                catch (FaultException<DivideByZeroException> exception)
                {

                }

                catch (FaultException<ExceptionDetail> eee)
                {

                }

                catch (FaultException<ErrorInfo> eee2)
                {
                    Console.WriteLine(HttpProxy.Divide(8, 2));
                }

                catch (CommunicationException ex)
                {
                    //FaultException fe = (FaultException)ex;
                }
            }

 

如果不定义  [FaultContract(typeof(ErrorInfo))]那么错误异常 将在客户端CommunicationException中截获 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值