wcf事务例子
1.记账单
service
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace DealerFund { public class DealerFundService : IDealerFund { //2必须启用事务范围 [OperationBehavior(TransactionScopeRequired=true)] public void AddFund(int money) { using (testEntities context = new testEntities()) { dt_dealerfund dealerFund =context.dt_dealerfund.FirstOrDefault(); dealerFund.currentMoney += money; context.SaveChanges(); } } } }
contract
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace DealerFund { [ServiceContract] public interface IDealerFund { [OperationContract] [TransactionFlow(TransactionFlowOption.Mandatory)]//3强制要求客户端传递事务 void AddFund(int money); } }
host
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using DealerFund; namespace DealerFundServiceHost { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(DealerFundService))) { try { host.Opened += new EventHandler(host_Opened); host.Open(); Console.ReadKey(); } catch (Exception ex) { host.Abort(); throw ex; } finally { host.Close(); } } } static void host_Opened(object sender, EventArgs e) { Console.WriteLine("服务已经监听。。。。。"); } } }
config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="testEntities1" connectionString="metadata=res://*/test.csdl|res://*/test.ssdl|res://*/test.msl;provider=System.Data.SqlClient;provider connection string="Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> <add name="testEntities2" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.serviceModel> <services> <service name="DealerFund.DealerFundService" behaviorConfiguration="dealerfundservicebehavior"> <endpoint address="net.tcp://localhost:9001/dealerfundservice" binding="netTcpBinding" contract="DealerFund.IDealerFund"></endpoint> </service> </services> <bindings> <netTcpBinding> <!--1事务的传播属性必须设置为true才能传播事务--> <binding transactionFlow="true"> <reliableSession enabled="true"/> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="dealerfundservicebehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9002/dealerfundservice/max"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
2.总金额
service
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace FundVoucher { public class FundVoucherService:IFundVoucher { [OperationBehavior(TransactionScopeRequired=true)] public void AddFundVoucher(int money) { using (testEntities context = new testEntities()) { context.dt_fundvoucher.AddObject(new dt_fundvoucher() { inputMoney = money }); context.SaveChanges(); } } } }
contract
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace FundVoucher { [ServiceContract] public interface IFundVoucher { [OperationContract] [TransactionFlow(TransactionFlowOption.Mandatory)] void AddFundVoucher(int money); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using FundVoucher; namespace FundVoucherServiceHost { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(FundVoucherService))) { try { host.Opened += new EventHandler(host_Opened); host.Open(); Console.Read(); } catch (Exception ex) { host.Abort(); throw ex; } finally { host.Close(); } } } static void host_Opened(object sender, EventArgs e) { Console.WriteLine("服务已经启动"); } } }
host
config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="testEntities1" connectionString="metadata=res://*/test.csdl|res://*/test.ssdl|res://*/test.msl;provider=System.Data.SqlClient;provider connection string="Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> <add name="testEntities2" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.serviceModel> <services> <service name="FundVoucher.FundVoucherService" behaviorConfiguration="fundvoucherserivcebehavior"> <endpoint address="net.tcp://localhost:9003/fundvoucherservice" binding="netTcpBinding" contract="FundVoucher.IFundVoucher"></endpoint> </service> </services> <bindings> <netTcpBinding> <binding transactionFlow="true"> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="fundvoucherserivcebehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9004/fundvoucherservice/mex"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
3.测试客户端
private void button2_Click(object sender, EventArgs e) { SRDealerFund.DealerFundClient client = new SRDealerFund.DealerFundClient(); SRFundVoucher.FundVoucherClient client2 = new SRFundVoucher.FundVoucherClient(); using (TransactionScope scope = new TransactionScope()) { client.AddFund(200); client2.AddFundVoucher(200); scope.Complete(); } }