[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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[WCF Transaction] 3. 事务投票
下一篇:[WCF Transaction] 1. 基本概念

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月03日 06时53分07秒