WCF开发工具功能特点比较突出,其优势突出的功能为它在开发领域中占据着一个比较重要的地位。在这里我们将会通过对WCF事务演示的解读,来充分的了解一下这一开发平台的应用方式。
公司主营业务:网站设计制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出分宜免费做网站回馈大家。
下面的这段代码就是WCF事务演示的经典示例:
- // -------- 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
.CreateChannel
(bindingClient,- new EndpointAddress("net.tcp://localhost:8080"));
- IService2 client2 = ChannelFactory
.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();
- }
- }
- }
- }
以上就是我们为大家带来的WCF事务演示。
分享标题:WCF事务演示经典实例剖析
当前地址:http://www.gawzjz.com/qtweb/news27/197727.html
成都网站建设公司_创新互联,为您提供微信公众号、品牌网站设计、Google、用户体验、标签优化、商城网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联