[WCF Transaction] 2. 事务演示
发布日期:2021-06-29 03:54:29
浏览次数:4
分类:技术文章
本文共 2459 字,大约阅读时间需要 8 分钟。
下面的代码演示了典型的 Client/Services Distributed Transaction。
// -------- Service1 ----------------- [ServiceContract] public interface IService1 { [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] void Test(); } public class MyService1 : IService1 { [OperationBehavior(TransactionScopeRequired=true)] public void Test() { string connStr = "server=(local);uid=sa;pwd=sa;database=temp"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand("insert into [User] ([name]) values (@name)", conn); cmd.Parameters.Add(new SqlParameter("@name", "ZhangSan")); cmd.ExecuteNonQuery(); } } } // -------- Service2 ----------------- [ServiceContract] public interface IService2 { [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] void Test(); } public class MyService2 : IService2 { [OperationBehavior(TransactionScopeRequired = true)] public void Test() { string connStr = "server=(local);uid=sa;pwd=sa;database=temp"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand("insert into Account ([user], [money]) values (@user, @money)", conn); cmd.Parameters.Add(new SqlParameter("@user", "ZhangSan")); cmd.Parameters.Add(new SqlParameter("@money", 100)); cmd.ExecuteNonQuery(); } } } public class WcfTest { public static void Test() { // -------- Host ----------------- AppDomain.CreateDomain("Server").DoCallBack(delegate { NetTcpBinding bindingServer = new NetTcpBinding(); bindingServer.TransactionFlow = true; ServiceHost host1 = new ServiceHost(typeof(MyService1), new Uri("net.tcp://localhost:8080")); host1.AddServiceEndpoint(typeof(IService1), bindingServer, ""); host1.Open(); ServiceHost host2 = new ServiceHost(typeof(MyService2), new Uri("net.tcp://localhost:8081")); host2.AddServiceEndpoint(typeof(IService2), bindingServer, ""); host2.Open(); }); // -------- Client ----------------- NetTcpBinding bindingClient = new NetTcpBinding(); bindingClient.TransactionFlow = true; IService1 client1 = ChannelFactory<IService1>.CreateChannel(bindingClient, new EndpointAddress("net.tcp://localhost:8080")); IService2 client2 = ChannelFactory<IService2>.CreateChannel(bindingClient, new EndpointAddress("net.tcp://localhost:8081")); using (TransactionScope scope = new TransactionScope()) { try { client1.Test(); client2.Test(); scope.Complete(); } finally { (client1 as IDisposable).Dispose(); (client2 as IDisposable).Dispose(); } } } }
转载地址:https://blog.csdn.net/zengjibing/article/details/3814344 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月10日 18时46分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
元宵节电商促销首页设计PSD分层模板
2019-04-29
APP设计灵感|高颜值时钟页面!让每一秒都过得有意义
2019-04-29
值得电商美工借鉴的购物APP页面设计,让人无法自拔
2019-04-29
电商产品页多种出彩表现设计手法!
2019-04-29
分布式与集成
2019-04-29
C#SUM函数改变数据精度问题
2019-04-29
机器翻译/注意力机制
2019-04-29
Transformer介绍
2019-04-29
SpringMVC异常处理之第三种:ExceptionHandler注解
2019-04-29
如何通过Eclipse来创建SpringBoot项目?
2019-04-29
Spring中 JavaConfig和常见注解
2019-04-29
SpringBoot启动类注解简要介绍
2019-04-29
Spring Boot扩展启动行为-改变启动Banner
2019-04-29
如何通过设置setting加快Maven 及更新SpringBoot项目的速度
2019-04-29
如何设置Spring Boot热部署
2019-04-29
Spring Boot整合Web开发-JSON
2019-04-29
Spring Boot整合Web开发-如何集合模板Thymeleaf?
2019-04-29
Spring Boot整合Web开发-freemarker
2019-04-29
Spring Boot整合Web开发之如何集成JSP
2019-04-29
全局异常处理之自定义全局错误页面、404及500错误页面
2019-04-29