wcf使用事物

-------------------ICustomer(接口类)------------------------------------

[ServiceContract]
    public interface ICustomer
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        int RegisterCustomer(Customer customer);//注册一个客户
    }


 ------------------IRental(接口类)----------------------------------------------
 

public interface IRental
    {
        [OperationContract]
        [FaultContract(typeof(RentalRegistFault))]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        string RegisterCarRental(RentalRegistraction rentalRegistraction);//注册一个租赁合同

------------------CustomerImplementation(客户实现类)----------------------------------------------
 

public class CustomerImplementation:ICustomer
    {
        #region ICustomer 成员

        [OperationBehavior( TransactionAutoComplete=true,TransactionScopeRequired=true)]
        public int RegisterCustomer(CustomerInterface.Customer customer)
        {
            using (DataClassesCustomerDataContext context = new DataClassesCustomerDataContext())
            {
                Customer customerForInsert;
                customerForInsert = new Customer();
                customerForInsert.CustomerFirstName = customer.CustomerFirstName;
                customerForInsert.CustomerName = customer.CustomerName;

                context.Customer.InsertOnSubmit(customerForInsert);
                context.SubmitChanges();

                return customerForInsert.CustomerID;
            }
        }

        #endregion
    }

------------------------------ RentalImplementation(实现类)-------------------------------------------------------------

public class RentalImplementation:IRental
    {
        #region IRental 成员
        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        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";

        }

 

------------------ExternalInterfaceFacadeImplementation(实现类)-----------------------------------

public class ExternalInterfaceFacadeImplementation : IExtenalInterface
    {
        #region IExtenalInterface 成员

        public void SubmitRentalContract(RentalContract rentalContract)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                NetNamedPipeBinding netNamedPipeBinding = new NetNamedPipeBinding();
                netNamedPipeBinding.TransactionFlow = true;

                //获取客户Service
                CustomerInterface.ICustomer icustomerService = ChannelFactory<CustomerInterface.ICustomer>.CreateChannel(netNamedPipeBinding, new EndpointAddress("net.pipe://localhost/customerservice"));
                int newCustomerID = icustomerService.RegisterCustomer(rentalContract.Customer);

                rentalContract.RentalRegistration.CustomerID = newCustomerID;

                //获取租赁Service
                RentalInterface.IRental iRentalService = ChannelFactory<RentalInterface.IRental>.CreateChannel(netNamedPipeBinding, new EndpointAddress("net.pipe://localhost/rentalservice"));
                iRentalService.RegisterCarRental(rentalContract.RentalRegistration);

                scope.Complete();
            }
        }

        #endregion
    }

-------------------------------config(配置)-----------------------------------------------

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name ="CarManagementService.CarManagementImplementation" behaviorConfiguration="ExposeMetaDataBehavior">
        <endpoint
           address="http://localhost:9876/CarManagementService"
           binding="wsHttpBinding"
           bindingConfiguration="AllowBigMessageSize"
           contract="CarManagementInterface.ICarManagement"
          />
      </service>

      <service name ="CustomerService.CustomerImplementation">
        <endpoint
           address="http://localhost:9876/CustomerService"
           binding="wsHttpBinding"
           contract="CustomerInterface.ICustomer"
          />
        <endpoint
          address="net.pipe://localhost/customerservice"
          binding="netNamedPipeBinding"
          bindingConfiguration="SupportTransactionNetNamedBinding"
          contract="CustomerInterface.ICustomer"
          >
          
        </endpoint>
      </service>

      <service name ="RentalService.RentalImplementation">
        <!--Http绑定的终结点-->
        <endpoint
           address="http://localhost:9876/RentalService"
           binding="wsHttpBinding"
           contract="RentalInterface.IRental"
          />

        <!--命名管道的终结点-->
        <endpoint
         address="net.pipe://localhost/rentalservice"
         binding="netNamedPipeBinding"
         bindingConfiguration="SupportTransactionNetNamedBinding"
         contract="RentalInterface.IRental"
          />
      </service>

      <service name ="ExternalInterfaceFacade.ExternalInterfaceFacadeImplementation" behaviorConfiguration="ExposeMetaDataBehaviorRentalFacade">
        <endpoint
           address="http://localhost:9876/ExternalInterfaceFacadeService"
           binding="wsHttpBinding"
           contract="ExtenalInterface.IExtenalInterface"
          />
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ExposeMetaDataBehavior">
          <serviceMetadata httpGetUrl="http://localhost:9876/CarManagementService/Mex" httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>

        <behavior name="ExposeMetaDataBehaviorRentalFacade">
          <serviceMetadata httpGetUrl="http://localhost:9876/RentalFacadeService/Mex" httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <wsHttpBinding>
        <binding name="AllowBigMessageSize" maxReceivedMessageSize="999999" >
        </binding>
      </wsHttpBinding>

      <netNamedPipeBinding>
        <binding name ="SupportTransactionNetNamedBinding" transactionFlow="true" >
        </binding>
      </netNamedPipeBinding>
      
    </bindings>
  </system.serviceModel>
</configuration>

----------------------------------client(客户端)---------------------------------------------------

private void button1_Click(object sender, EventArgs e)
        {
            RentalFacade.ExtenalInterfaceClient client = new RentalFacade.ExtenalInterfaceClient();

            RentalContract contract = new RentalContract();

            Customer c = new Customer();
            c.CustomerName = DateTime.Now.ToString();
            c.CustomerFirstName = "cc";
            c.CustomerBirthDate = DateTime.Now;
            
            contract.Customer = c;

            RentalRegistraction registraction = new RentalRegistraction();
            registraction.CarID = "carid";
            registraction.Comments = "comments";
            registraction.DropOffDateTime = DateTime.Now;
            registraction.DropOffLocation = 3;
            registraction.PaymentStatus = PaymentStatusEnum.INV;
            registraction.PickupDateTime = DateTime.Now;
            registraction.PickUpLocation = 2;

            contract.RentalRegistration = registraction;

            client.SubmitRentalContract(contract);
        }

posted @ 2013-10-11 13:50  feidaochuanqing  阅读(215)  评论(0编辑  收藏  举报