wcf服务
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace MyWcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] string ExecuteSql(string value); } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace MyWcfService { [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete = false, InstanceContextMode = InstanceContextMode.PerCall)] public class Service1 : IService1 { [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)] public string ExecuteSql(string value) { SqlHelper sql = new SqlHelper(); return sql.ExecuteNonQuery(value).ToString();//如果是mysql帮助文档中的此方法,会报错,因为mysql数据库不支持分布式事务 } } }
宿主serviceHost
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyWcfService; namespace serviceHost { class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(Service1)); host.Open(); Console.WriteLine("open"); Console.ReadKey(); } } }
宿主的配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="serviceDebuBehavior"> <!--可以在客户端获取异常--> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="MyWcfService.Service1" behaviorConfiguration="serviceDebuBehavior"> <!--以上异常配置--> <endpoint address="net.tcp://127.0.0.1:3721/calculatorservice" binding="netTcpBinding" contract="MyWcfService.IService1" bindingConfiguration="transactionBinding" /> </service> </services> <bindings> <netTcpBinding> <!--transactionFlow=true 开启事务 必须配置,而且宿主和客户端都要配置--> <binding name="transactionBinding" transactionFlow="true" > <reliableSession enabled="true"/> <security></security> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
//客户端 调用者ClinetAPP
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Transactions; using MyWcfService; namespace ClinetAPP { class Program { static void Main(string[] args) { ChannelFactory<IService1> channl = new ChannelFactory<IService1>("WcfService"); try { IService1 service = channl.CreateChannel(); using (TransactionScope trans = new TransactionScope()) { string sql1 = "insert into demo values(‘1‘,‘2‘)"; string sql2 = "update demo set pwd2=‘2‘"; string s1 = service.ExecuteSql(sql1); string s2 = service.ExecuteSql(sql2); Console.WriteLine(s1 + s2); Console.ReadKey(); trans.Complete();//此处提交事务。如果没有执行此方法,事务就会回滚。 } } catch (Exception ex) { } } } }
//客户端配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="WcfService" address="net.tcp://127.0.0.1:3721/calculatorservice" binding="netTcpBinding" contract="MyWcfService.IService1" bindingConfiguration="transactionBinding"/> </client> <bindings> <netTcpBinding> <binding name="transactionBinding" transactionFlow="true" > <reliableSession enabled="true"/> <security></security> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
wcf事务
时间: 2024-10-21 15:12:48