-------------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文件