[WCF Transaction] 2. 事务演示
发布日期:2021-06-29 03:54:29
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月03日 06时53分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
测一测|企业信息安全管理10道小题你能答对几个?
2019-04-29
信息安全意识主题分享-数据安全
2019-04-29
请收下这份CCSP云安全专家认证备考攻略(合集)
2019-04-29
一文带你了解PDPF隐私与数据保护认证
2019-04-29
WireShark网络安全分析
2019-04-29
【备考干货】一次拿下CISM认证的经验分享
2019-04-29
疫情之下看我如何调整心态拿下CISM认证
2019-04-29
2020年Security+备考经验分享
2019-04-29
工具集电子书等4G资料免费分享(IT 信安菜鸟入门必备)
2019-04-29
备考干货「三」CISM(国际注册信息安全经理)考过笔记
2019-04-29
大学生有哪些信息安全认证可以考?
2019-04-29
2020年Security+备考经验分享(二)
2019-04-29
ITIL4,全球IT行业抢手的资格认证之一
2019-04-29
CRISC备考重点知识解读
2019-04-29
推荐信息安全书籍27本(含电子书)
2019-04-29
攻防对抗蓝队必修课——文末有文档模板分享
2019-04-29
云计算领域的安全威胁如何应对?
2019-04-29
你了解三证合一的数据保护官(DPO)吗?
2019-04-29
小白入门网络安全,需要学习哪些内容?
2019-04-29
如何找回丢失的比特币
2019-04-29