WCF分布式事务

http://technet.microsoft.com/zh-cn/magazine/ee126350(VS.100).aspx

http://hi.baidu.com/yyylajzwwfbegpe/item/1a426c0add1f596dd55a119f

http://support.microsoft.com/kb/899191

http://www.cnblogs.com/chnking/archive/2010/01/10/1643384.html
http://msdn.microsoft.com/zh-cn/library/cc949057.aspx
http://technet.microsoft.com/zh-cn/magazine/ms752261(VS.90).aspx


1.契约

    /// <summary>
    /// 服务契约
    /// </summary>
    [ServiceContract(SessionMode=SessionMode.Required)]
    public partial interface IContract
    {

        [OperationContract(Name = "AddInfo")]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [FaultContract(typeof (WcfFaultInfo))]
        bool Add(Info info);

   }

2.服务端实现

    /// <summary>
    /// 服务
    /// </summary>
    [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete = false,
        InstanceContextMode = InstanceContextMode.PerCall
        , ConcurrencyMode = ConcurrencyMode.Multiple), ErrorHandlerBehavior]
    public partial class Service:IContract
    {

        /// <summary>
        /// 添加一条Info信息
        /// </summary>
        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool Add(Info info)
        {

            //实现代码

        }

   }

2.1服务端的WEB.config

   <system.serviceModel>
    <services>
      <service behaviorConfiguration="transactionBehavior" name="Service">
        <endpoint address="net.tcp://localhost:1234/IContract" binding="netTcpBinding" bindingConfiguration="TcpBind" contract="IContract"/>
      </service>
    </services>
    <behaviors>
      <!--netTcpBinding-->
      <serviceBehaviors>
        <behavior name="transactionBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceTimeouts transactionTimeout="00:02:00"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceThrottling maxConcurrentCalls="5000"  maxConcurrentSessions="1000" maxConcurrentInstances="1000" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="TcpBind" closeTimeout="00:03:00" sendTimeout="00:03:00" openTimeout="00:03:00"  maxBufferSize="2147483647" maxConnections="50" maxReceivedMessageSize="2147483647" transactionFlow="true" receiveTimeout="00:10:00"  transactionProtocol="OleTransactions"  >
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <reliableSession enabled="true" ordered ="true" inactivityTimeout="00:30:00" />
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

3.客户端实现

    /// <summary>
    /// 服务客户端
    /// </summary>
    internal partial class SyncHandle : ClientBase<IContract>,IContract
    {

       public bool Add(Info info)
        {

            //实现代码

             try
            {
                return Channel.Add(info);
            }
            catch (FaultException<WcfFaultInfo> ex)
            {
                Abort();
                ex.ExceptionHandle();
            }
            catch (FaultException ex)
            {
                Abort();
                ex.ExceptionHandle();
            }
            catch (CommunicationException ex)
            {
                Abort();
                LogFunc.AddLog(ex, "CommunicationException--");
            }
            catch (Exception ex)
            {
                Abort();
                LogFunc.AddLog(ex, "客户端异常");
            }   

        }

    } 

3.1客户端web.config

<system.serviceModel>
    <client>
      <endpoint behaviorConfiguration="tcpbehavior" address="net.tcp://localhost:1234/IContract" binding="netTcpBinding" bindingConfiguration="TcpBind" contract="IContract" name="EndPoint"/>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="tcpbehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="TcpBind" sendTimeout="00:03:00" maxBufferSize="2147483647" maxConnections="50" maxReceivedMessageSize="2147483647" transactionFlow="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <reliableSession enabled="true" ordered="true" inactivityTimeout="00:30:00"/>
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>


3.2客户端调用

 /// <summary>
    /// 客户端
    /// </summary>
    public partial class SyncClient
    {
        private const string ENDPOINTNAME = "EndPoint"; //WCF终结点

        /// <summary>
        /// 添加一条信息
        /// </summary>     
        /// <returns></returns>
        public static bool Add(Info info)
        {

var proxy = new SyncHandle(ENDPOINTNAME);
            bool binfo;
            try
            {
                info = proxy.Add(info);
            }
            //catch (FaultException<WcfFaultInfo> ex)
            //{
            //    throw ex;
            //}
            //catch(Exception ex)
            //{
            //    throw ex;
            //}
            finally
            {
                proxy.Close();
            }
            return binfo;


        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值