首页 > 其他 > 详细

wcf事务例子

时间:2014-04-17 23:03:38      阅读:609      评论:0      收藏:0      [点我收藏+]

1.记账单

service

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace DealerFund
{
    public class DealerFundService : IDealerFund
    {
        //2必须启用事务范围
        [OperationBehavior(TransactionScopeRequired=true)]
        public void AddFund(int money)
        {
            using (testEntities context = new testEntities())
            {
                dt_dealerfund dealerFund =context.dt_dealerfund.FirstOrDefault();
                dealerFund.currentMoney += money;
                context.SaveChanges();
            }
        }
    }
}
bubuko.com,布布扣

 

contract

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace DealerFund
{
    [ServiceContract]
    public interface IDealerFund
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]//3强制要求客户端传递事务
        void AddFund(int money);
    }
}
bubuko.com,布布扣

 

host

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using DealerFund;

namespace DealerFundServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(DealerFundService)))
            {
                try
                {
                    host.Opened += new EventHandler(host_Opened);
                    host.Open();
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    host.Abort();
                    throw ex;
                }
                finally
                {
                    host.Close();
                }
            }
        }

        static void host_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("服务已经监听。。。。。");
        }
    }
}
bubuko.com,布布扣

 

config

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="testEntities1" connectionString="metadata=res://*/test.csdl|res://*/test.ssdl|res://*/test.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="testEntities2" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  
  <system.serviceModel>
    <services>
      <service name="DealerFund.DealerFundService" behaviorConfiguration="dealerfundservicebehavior">
        <endpoint address="net.tcp://localhost:9001/dealerfundservice" binding="netTcpBinding" contract="DealerFund.IDealerFund"></endpoint>
      </service>
    </services>

    <bindings>
      <netTcpBinding>
        <!--1事务的传播属性必须设置为true才能传播事务-->
        <binding transactionFlow="true">
          <reliableSession enabled="true"/>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="dealerfundservicebehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9002/dealerfundservice/max"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    
  </system.serviceModel>
</configuration>
bubuko.com,布布扣

 

2.总金额

service

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace FundVoucher
{
    public class FundVoucherService:IFundVoucher
    {
        [OperationBehavior(TransactionScopeRequired=true)]
        public void AddFundVoucher(int money)
        {
            using (testEntities context = new testEntities())
            {
                context.dt_fundvoucher.AddObject(new dt_fundvoucher() {  inputMoney = money });
                context.SaveChanges();
            }
        }
    }
}
bubuko.com,布布扣

 

contract

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace FundVoucher
{
    [ServiceContract]
    public interface IFundVoucher
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Mandatory)]
        void AddFundVoucher(int money);
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using FundVoucher;

namespace FundVoucherServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(FundVoucherService)))
            {
                try
                {
                    host.Opened += new EventHandler(host_Opened);
                    host.Open();
                    Console.Read();
                }
                catch (Exception ex)
                {
                    host.Abort();
                    throw ex;
                }
                finally
                {
                    host.Close();
                }
            }
        }

        static void host_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("服务已经启动");
        }
    }
}
bubuko.com,布布扣

 

host

config

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="testEntities1" connectionString="metadata=res://*/test.csdl|res://*/test.ssdl|res://*/test.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="testEntities2" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WIN-IF9U3EJTK7N\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=lizhch;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  
  <system.serviceModel>
    <services>
      <service name="FundVoucher.FundVoucherService"  behaviorConfiguration="fundvoucherserivcebehavior">
        <endpoint address="net.tcp://localhost:9003/fundvoucherservice" binding="netTcpBinding" contract="FundVoucher.IFundVoucher"></endpoint>
      </service>
    </services>

    <bindings>
      <netTcpBinding>
        <binding transactionFlow="true">
        </binding>
      </netTcpBinding>
    </bindings>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="fundvoucherserivcebehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9004/fundvoucherservice/mex"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  </system.serviceModel>
</configuration>
bubuko.com,布布扣

 

3.测试客户端

bubuko.com,布布扣
private void button2_Click(object sender, EventArgs e)
        {
            SRDealerFund.DealerFundClient client = new SRDealerFund.DealerFundClient();
            SRFundVoucher.FundVoucherClient client2 = new SRFundVoucher.FundVoucherClient();

            using (TransactionScope scope = new TransactionScope())
            {
                client.AddFund(200);
                client2.AddFundVoucher(200);
                scope.Complete();
            }
        }
bubuko.com,布布扣

 

wcf事务例子,布布扣,bubuko.com

wcf事务例子

原文:http://www.cnblogs.com/feidaochuanqing/p/3669628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!