WCF事物例子

1.Contract

namespace BankContract
{

    [ServiceContract]
    public interface IBank
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]//设置事务流,必须启用使用
        void IntoMoney(int money);

        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        void OutMoney(int money);
    }
}

2.Service

namespace BankService
{
    [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete=false,InstanceContextMode = InstanceContextMode.Single)]
    public class Bank:IBank
    {
        #region IBank 成员

        [OperationBehavior(TransactionScopeRequired=true)]
        public void IntoMoney(int money)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = @"Data Source=LENOVO-120129K2\SQLEXPRESS1;Initial Catalog=Bank;User ID=sa;Password=lizhch";
                conn.Open();

                string str = "UPDATE account set last = last - " + money + " WHERE id = 'a'";

                SqlCommand cmd = new SqlCommand(str, conn);
                cmd.ExecuteNonQuery();
            }
        }

        [OperationBehavior(TransactionScopeRequired = true)]
        public void OutMoney(int money)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = @"Data Source=LENOVO-120129K2\SQLEXPRESS1;Initial Catalog=Bank;User ID=sa;Password=lizhch";
                conn.Open();

                string str = "UPDATE account set last = last + " + money + " WHERE id = 'b'";

                SqlCommand cmd = new SqlCommand(str, conn);
                cmd.ExecuteNonQuery();

            }
        }

        #endregion
    }
}

3.Host

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Uri uri = new Uri("http://localhost:8989/Bank");
            ServiceHost host = new ServiceHost(typeof(Bank), uri);

            WSDualHttpBinding bs = new WSDualHttpBinding();
            bs.TransactionFlow = true;
            host.AddServiceEndpoint(typeof(IBank), bs,"");
            host.Opened += new EventHandler(host_Opened);
            host.Open();
        }

        void host_Opened(object sender, EventArgs e)
        {
            this.Text = "服务已启动";
        }
    }

4.Client

 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WSDualHttpBinding binding = new WSDualHttpBinding();
            binding.TransactionFlow = true;
            binding.ClientBaseAddress = new Uri("http://localhost:9999/Bank");

            EndpointAddress address = new EndpointAddress("http://localhost:8989/Bank");
            ChannelFactory<IBank> factory = new ChannelFactory<IBank>(binding, address);

            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    factory.CreateChannel().IntoMoney(100);
                    factory.CreateChannel().OutMoney(100);
                    scope.Complete();
                    MessageBox.Show("保存成功");
                }
                catch (Exception ex)
                {
                    Transaction.Current.Rollback();
                    MessageBox.Show(ex.Message);
                }
               
            }

           
        }

posted @ 2013-04-06 22:42  feidaochuanqing  阅读(185)  评论(0编辑  收藏  举报