技术开发 频道

.NET实用技巧—NHibernate分布式事务

  ②.Order

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  ..........

  
<!--spring配置-->
  
<spring xmlns="http://www.springframework.net">
    
<parsers>
      
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
      
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
    
</parsers>
    
<context>
      
<resource uri="config://spring/objects" />

      
<!--Dao-->
      
<resource uri="assembly://Order.Dao/Order.Dao.Config/Dao.xml" />
      
<!--Service-->
      
<resource uri="assembly://Order.Service/Order.Service.Config/Service.xml" />

    
</context>
    
<objects xmlns="http://www.springframework.net"
             xmlns:aop
="http://www.springframework.net/aop">

      
<object id="Order.Host" type="Order.Host.Implement.OrderServer, Order.Host">
        
<property name="Manager" ref="Order.OrderManager" />
      
</object>

    
</objects>
  
</spring>

  
<appSettings>
    
<add key="Spring.Data.NHibernate.Support.SessionScope.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
  
</appSettings>

  
<system.web>
    
<compilation debug="true" targetFramework="4.0" />

    
<httpModules>
      
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
    
</httpModules>

  
</system.web>
  
<system.serviceModel>
    
<services>
      
<service name="Order.Host">
        
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="ServerBinding" contract="Order.Host.IOrderContract"/>
        
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      
</service>
    
</services>
    
<bindings>
      
<wsHttpBinding >
        
<binding name="ServerBinding" transactionFlow="true"  >
        
</binding>
      
</wsHttpBinding>
    
</bindings>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior>
          
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
          
<serviceMetadata httpGetEnabled="true"/>
          
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          
<serviceDebug includeExceptionDetailInFaults="true"/>
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
    
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
  
</system.serviceModel>
<system.webServer>
    
<modules runAllManagedModulesForAllRequests="true"/>
  
</system.webServer>
  
</configuration>

<%@ ServiceHost Language="C#" Debug="true" Service="Order.Host" Factory="Spring.ServiceModel.Activation.ServiceHostFactory"%>

   四、客户端

[TestFixture]
    
public class HostTest
    {
        
private CustomerContractClient customerProxy;

        
private OrderContractClient orderProxy;

        [SetUp]
        
public void Init()
        {
            customerProxy
= new CustomerContractClient();
            orderProxy
= new OrderContractClient();
        }

        [Test]
        
public void InitData()
        {
            using (TransactionScope scope
= new TransactionScope())
            {
                customerProxy.Save(
new CustomerInfo
                {
                    Name
= "刘冬"
                });

                scope.Complete();
            }
        }

        [Test]
        
public void DistributedTransactionTest()
        {
            using (TransactionScope scope
= new TransactionScope())
            {
                try
                {
                    CustomerInfo customer
= customerProxy.Get(1);
                    orderProxy.Save(
new OrderInfo
                    {
                        Address
= "中国北京",
                        CustomerId
= (int)customer.ID,
                        OrderDate
= DateTime.Now
                    });
                    customer.Money
+= 1000;
                    customerProxy.Update(customer);
                    scope.Complete();
                    Console.WriteLine(
"分布式事务已提交");
                }
                catch (Exception ex)
                {
                    Transaction.Current.Rollback();
                    Console.WriteLine(
"发送错误:分布式事务已回滚");
                }
            }
        }
    }

  

0
相关文章