博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF自定义异常
阅读量:5144 次
发布时间:2019-06-13

本文共 2799 字,大约阅读时间需要 9 分钟。

-------------interface----------------------

  /// <summary>

    /// 租赁合同管理
    /// </summary>
    [ServiceContract]
    public interface IRental
    {
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        string RegisterCarRental(RentalRegistraction rentalRegistraction);//注册一个租赁合同
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        void RegisterCarRentalAsPaid(string rentalID);//标记租赁合同已付款
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        void StartCarRental(string rentalID);//在某个地点为某个合同准备好汽车
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        void StopCarRental(string rentalID);//车辆将指定的位置还给公司
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        RentalRegistraction GetRentalRegsitration(string rentalID);//获取某个合同的全部数据
    }

---------------------RentalRegistFault--------------------------

   [DataContract]

    public class RentalRegistFault
    {
        [DataMember]
        public string FaultDescription { get; set; }
        [DataMember]
        public int FaultID { get; set; }
    }

------------------Service_Imp-------------------------------

 public class RentalImplementation:IRental

    {
        #region IRental 成员

        public string RegisterCarRental(RentalRegistraction rentalRegistraction)

        {
            if (rentalRegistraction == null)
            {
                RentalRegistFault fault = new RentalRegistFault();
                fault.FaultID = 1;
                fault.FaultDescription = "传递的参数为空";
                throw new FaultException<RentalRegistFault>(fault,"传递的参数引用为空");
            }

            using (DataClassesRentalDataContext context = new DataClassesRentalDataContext())

            {
                Rental rental = new Rental();
                rental.CarID = rentalRegistraction.CarID;
                rental.CustomerID = rentalRegistraction.CustomerID;
                rental.DropOffLocation = rentalRegistraction.DropOffLocation;

                context.Rental.InsertOnSubmit(rental);

                context.SubmitChanges();
            }
            return "ok";

        }

----------------------------Client-------------------------------------

private void button1_Click(object sender, EventArgs e)

        {
            CarRentalPrxoxy proxy = new CarRentalPrxoxy();

            RentalRegistraction registraction = new RentalRegistraction();

            registraction.CarID = "JF00000";
            registraction.Comments = "备注";
            registraction.CustomerID = 1;
            registraction.DropOffDateTime = DateTime.Now;
            registraction.DropOffLocation = -1;

            string reult = "";

            try

            {
                reult = proxy.RegisterCarRental(null);
            }
            catch (FaultException<RentalInterface.RentalRegistFault> rentalRegistFaultex)
            {
                RentalRegistFault fault = rentalRegistFaultex.Detail;
                MessageBox.Show(rentalRegistFaultex.Message + " "+ fault.FaultDescription+" " + fault.FaultID);
                return;
            }
            catch (FaultException ex)
            {
                MessageBox.Show(ex.StackTrace);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                return;

            }

            MessageBox.Show(reult);

        }

--------------------------------------------------------------------------

注意:FaultException需要引入System.ServiceModel.dll文件

转载于:https://www.cnblogs.com/feidaochuanqing/p/3362856.html

你可能感兴趣的文章
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
判断线段是否相交
查看>>
Codeforces Round #277 (Div. 2)
查看>>
一步步学Mybatis-搭建最简单的开发环境-开篇(1)
查看>>
微信小程序图片上传
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>
centos6.7 配置外网端口映射
查看>>
红外通信基础(含代码)
查看>>
淡定,啊。数据唯一性
查看>>
java并发编程之lock锁
查看>>
深入理解 JavaScript 事件循环(一)— event loop
查看>>
Hive(7)-基本查询语句
查看>>
常用第三方(分享,支付,二维码,语音,推送)
查看>>
Redis快速入门
查看>>
动态绑定时的显示隐藏控制
查看>>
注意java的对象引用
查看>>
C++ 面向对象 类成员函数this指针
查看>>
inline函数的总结
查看>>