集成企业服务事务性组件

Windows Communication Foundation (WCF) 提供了用于与 Enterprise Services 集成的自动机制(请参阅与 COM+ 应用程序集成)。 但是,你可能希望灵活地开发可在内部使用承载于企业服务中的事务性组件的服务。 由于 WCF 事务功能是在 System.Transactions 基础结构上生成的,因此将企业服务与 WCF 集成的过程与指定 System.Transactions 和企业服务之间的互操作性的过程相同,如与企业服务和 COM+ 事务的互操作性中所述。

若要在传入的流式事务和 COM+ 上下文事务之间提供所需级别的互操作性,服务实现必须创建一个 TransactionScope 实例并使用 EnterpriseServicesInteropOption 枚举中的适当值。

使企业服务与服务操作相集成

下面的代码演示对 Allowed 事务流执行的操作,该操作创建一个具有 TransactionScope 选项的 Full。 以下条件适用于这种情况:

任何其他方法调用也发生在同一个操作的事务范围内。

[ServiceContract()]  
public interface ICustomerServiceContract  
{  
   [OperationContract]  
   [TransactionFlow(TransactionFlowOption.Allowed)]  
   void UpdateCustomerNameOperation(int customerID, string newCustomerName);  
}  
  
[ServiceBehavior(TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable)]  
public class CustomerService : ICustomerServiceContract  
{  
   [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]  
   public void UpdateCustomerNameOperation(int customerID, string newCustomerName)  
   {  
   // Create a transaction scope with full ES interop  
      using (TransactionScope ts = new TransactionScope(  
                     TransactionScopeOption.Required,  
                     new TransactionOptions(),  
                     EnterpriseServicesInteropOption.Full))  
      {  
         // Create an Enterprise Services component  
         // Call UpdateCustomer method on an Enterprise Services
         // component
  
         // Call UpdateOtherCustomerData method on an Enterprise
         // Services component
         ts.Complete();  
      }  
  
      // Do UpdateAdditionalData on an non-Enterprise Services  
      // component  
   }  
}  

如果某一操作的当前事务和对事务性企业服务组件的调用之间不需要同步,则在实例化 None 实例时请使用 TransactionScope 选项。

使企业服务与客户端相集成

下面的代码演示使用 TransactionScope 实例的客户端代码,该实例具有 Full 设置。 在这种情况下,对支持事务流的服务操作的调用与对企业服务组件的调用发生在同一个事务范围内。

static void Main()  
{  
    // Create a client  
    CalculatorClient client = new CalculatorClient();  
  
    // Create a transaction scope with full ES interop  
    using (TransactionScope ts = new TransactionScope(  
          TransactionScopeOption.Required,  
          new TransactionOptions(),  
          EnterpriseServicesInteropOption.Full))  
    {  
        // Call Add calculator service operation  
  
        // Create an Enterprise Services component  
  
        // Call UpdateCustomer method on an Enterprise Services
        // component
  
        ts.Complete();  
    }  
  
    // Closing the client gracefully closes the connection and
    // cleans up resources  
    client.Close();  
}  

请参阅