WCF事務(wù)演示經(jīng)典實例剖析
作者:佚名 
  我們今天就為大家列舉了一個比較經(jīng)典的WCF事務(wù)演示,以方便大家對這方面的知識有一個詳細的了解,并加深對WCF的認知程度。
 WCF開發(fā)工具功能特點比較突出,其優(yōu)勢突出的功能為它在開發(fā)領(lǐng)域中占據(jù)著一個比較重要的地位。在這里我們將會通過對WCF事務(wù)演示的解讀,來充分的了解一下這一開發(fā)平臺的應(yīng)用方式。
下面的這段代碼就是WCF事務(wù)演示的經(jīng)典示例:
- // -------- 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();
 - }
 - }
 - }
 - }
 
以上就是我們?yōu)榇蠹規(guī)淼腤CF事務(wù)演示。
【編輯推薦】
責(zé)任編輯:曹凱 
                    來源:
                    豆豆網(wǎng)
 














 
 
 
 
 
 
 