System.Transactions 命名空间

使用 System.Transactions 命名空间包含的类可以编写自己的事务应用程序和资源管理器。 The System.Transactions namespace contains classes that allow you to write your own transactional application and resource manager. 具体地说,可以创建和参与(与一个或多个参与者)本地或分布式事务。 Specifically, you can create and participate in a transaction (local or distributed) with one or multiple participants.


描述可提交的事务。Describes a committable transaction.


描述事务的克隆,该克隆保证在应用程序停止事务上的工作之后才能提交事务。Describes a clone of a transaction providing guarantee that the transaction cannot be committed until the application comes to rest regarding work on the transaction. 无法继承此类。This class cannot be inherited.


在事务的管理已升级到 MSDTC 时 System.Transactions 所要求的权限。The permission that is demanded by System.Transactions when management of a transaction is escalated to MSDTC. 此类不能被继承。This class cannot be inherited.


允许对要使用声明性安全应用到代码中的 DistributedTransactionPermission 执行安全操作。Allows security actions for DistributedTransactionPermission to be applied to code using declarative security. 无法继承此类。This class cannot be inherited.


促进登记的事务参与者和事务管理器之间在事务的最后阶段中的通信。Facilitates communication between an enlisted transaction participant and the transaction manager during the final phase of the transaction.


促进登记的事务参与者与事务管理器之间在事务的准备阶段中的通信。Facilitates communication between an enlisted transaction participant and the transaction manager during the Prepare phase of the transaction.


提供一组回调,这组促进为单阶段提交而登记的参与者与食物管理器之间在收到 SinglePhaseCommit(SinglePhaseEnlistment) 通知时的通信。Provides a set of callbacks that facilitate communication between a participant enlisted for Single Phase Commit and the transaction manager when the SinglePhaseCommit(SinglePhaseEnlistment) notification is received.


表示可以委托的一个非根事务。Represents a non-rooted transaction that can be delegated. 无法继承此类。This class cannot be inherited.


表示事务。Represents a transaction.


在已经回滚的事务上尝试某个操作,或尝试提交事务而事务中止时引发的异常。The exception that is thrown when an operation is attempted on a transaction that has already been rolled back, or an attempt is made to commit the transaction and the transaction aborts.


为下面的事务事件提供数据:DistributedTransactionStartedTransactionCompletedProvides data for the following transaction events: DistributedTransactionStarted, TransactionCompleted.


在不能接受新工作的事务上尝试执行工作时引发的异常。The exception that is thrown when you attempt to do work on a transaction that cannot accept new work.


在不确定的事务上尝试某个操作,或尝试提交事务而事务变为 InDoubt 时引发的异常。The exception that is thrown when an operation is attempted on a transaction that is in doubt, or an attempt is made to commit the transaction and the transaction becomes InDoubt.


提供关于事务的附加信息。Provides additional information regarding a transaction.


促进 System.Transactions 和以前编写的用于与 MSDTC、COM+ 或 System.EnterpriseServices 进行交互的组件之间的交互。Facilitates interaction between System.Transactions and components that were previously written to interact with MSDTC, COM+, or System.EnterpriseServices. 无法继承此类。This class cannot be inherited.


包含用于事务管理的方法。Contains methods used for transaction management. 无法继承此类。This class cannot be inherited.


当资源管理器无法与事务管理器进行通信时引发的异常。The exception that is thrown when a resource manager cannot communicate with the transaction manager.


提升失败时引发的异常。The exception that is thrown when a promotion fails.


使代码块成为事务性代码。Makes a code block transactional. 无法继承此类。This class cannot be inherited.



包含指定事务行为的附加信息。Contains additional information that specifies transaction behaviors.



描述 DTC 事务。Describes a DTC transaction.


描述一个接口,资源管理器应实现该接口以在登记参与时为事务管理器提供两阶段提交通知回调。Describes an interface that a resource manager should implement to provide two phase commit notification callbacks for the transaction manager upon enlisting for participation.


描述作为资源管理器内部非分布式事务的提交委托的对象。Describes an object that acts as a commit delegate for a non-distributed transaction internal to a resource manager.


表示非根事务但是能够升级为由 MSDTC 管理的事务。Represents a transaction that is not a root transaction, but can be escalated to be managed by the MSDTC.


描述支持单阶段提交优化以参与事务的资源对象。Describes a resource object that supports single phase commit optimization to participate in a transaction.


描述可在需要时升级为由 MSDTC 管理的现有事务的委托事务。Describes a delegated transaction for an existing transaction that can be escalated to be managed by the MSDTC when needed.



控制要创建的依赖事务的种类。Controls what kind of dependent transaction to create.


确定是否应该在准备阶段登记该对象。Determines whether the object should be enlisted during the prepare phase.


指定分布式事务与 COM+ 事务进行交互的方式。Specifies how distributed transactions interact with COM+ transactions.


指定事务的隔离级别。Specifies the isolation level of a transaction.


指示是否为 TransactionScope 启用跨线程连续任务的事务流。Specifies whether transaction flow across thread continuations is enabled for TransactionScope.


提供用于创建事务范围的附加选项。Provides additional options for creating a transaction scope.


描述分布式事务的当前状态。Describes the current status of a distributed transaction.



为宿主环境提供一种机制,使其提供自己的默认 Current 概念。Provides a mechanism for the hosting environment to supply its own default notion of Current.


表示处理 TransactionCompleted 类的 Transaction 事件的方法。Represents the method that handles the TransactionCompleted event of a Transaction class.


表示将处理 DistributedTransactionStarted 类的 TransactionManager 事件的方法。Represents the method that will handle the DistributedTransactionStarted event of a TransactionManager class.


System.Transactions 基础结构通过支持在 SQL Server、ADO.NET、MSMQ 和 Microsoft 分布式事务处理协调器(MSDTC)中启动的事务,使事务编程在整个平台中都简单有效。The System.Transactions infrastructure makes transactional programming simple and efficient throughout the platform by supporting transactions initiated in SQL Server, ADO.NET, MSMQ, and the Microsoft Distributed Transaction Coordinator (MSDTC). 它提供基于 Transaction 类的显式编程模型,以及使用 TransactionScope 类的隐式编程模型,在该模型中,事务由基础结构自动管理。It provides both an explicit programming model based on the Transaction class, as well as an implicit programming model using the TransactionScope class, in which transactions are automatically managed by the infrastructure. 强烈建议使用易于开发的隐式模型。It is highly recommended that you use the easier implicit model for development. 若要开始使用,请参阅使用事务范围实现隐式事务主题。To get started, see the Implementing An Implicit Transaction Using Transaction Scope topic. 有关编写事务应用程序的详细信息,请参阅编写事务应用程序For more information on writing a transactional application, see Writing A Transactional Application.

System.Transactions 还提供了用于实现资源管理器的类型。System.Transactions also provides types for you to implement a resource manager. System.Transactions 基础结构的本地事务管理器允许使用可变资源或单个持久资源登记来有效地提交或回滚。The transaction manager native to the System.Transactions infrastructure allows volatile resources or a single durable resource enlistment to commit or roll back efficiently. 有关实现资源管理器的详细信息,请参阅实现资源管理器For more information on implementing a resource manager, see Implementing A Resource Manager.

当其他持久资源管理器使用事务自行登记时,事务管理器还会通过基于磁盘的事务管理器(例如 DTC)进行协调,以透明方式将本地事务升级到分布式事务。The transaction manager also transparently escalates local transactions to distributed transactions by coordinating through a disk-based transaction manager like the DTC, when an additional durable resource manager enlists itself with a transaction. System.Transactions 基础结构提供增强性能的关键方式有两种。There are two key ways that the System.Transactions infrastructure provides enhanced performance.

  • 动态升级,这意味着 System.Transactions 基础结构仅在事务实际需要时才会使用 MSDTC。Dynamic Escalation, which means that the System.Transactions infrastructure only engages the MSDTC when it is actually required for a transaction. 事务管理升级主题中深入介绍了此区域。This area is covered in depth in the Transaction Management Escalation topic.

  • 可提升的登记,如果某个资源是参与事务的唯一实体,则允许该资源(如数据库)取得事务的所有权。Promotable Enlistments, which allows a resource, such as a database, to take ownership of the transaction if it is the only entity participating in the transaction. 稍后,如果需要,System.Transactions 基础结构仍可将事务管理升级到 MSDTC。Later, if needed, the System.Transactions infrastructure can still escalate the management of the transaction to MSDTC. 这样进一步减少了使用 MSDTC 的机会。This further reduces the chance of using the MSDTC. 使用单阶段提交和可提升的单阶段通知主题,深入了解了该区域。This area is covered in depth in the Optimization Using Single Phase Commit and Promotable Single Phase Notification topic.

System.Transactions 定义了三个信任级别,它们限制了对它所公开的资源类型的访问。System.Transactions defines three levels of trust that restrict access on the types of resources it exposes. 具体而言,可能是由部分信任的代码调用了system.web程序集,因为该程序集已使用 AllowPartiallyTrustedCallers 特性(APTCA)进行标记。Specifically, the System.Transactions assembly can be called by partially trusted code as it has been marked with the AllowPartiallyTrustedCallers attribute (APTCA). 此特性实质上是移除了 LinkDemand 权限集的隐式 FullTrust 权限,该权限在其他情况下会被自动置于每个类型的每个可公共访问的方法上。This attribute essentially removes the implicit LinkDemand for the FullTrust permission set that is otherwise automatically placed on each publicly accessible method in each type. 但是,某些类型和成员还是需要更强的权限。However, some types and members still require stronger permissions.

下面列出了部分受信任的代码无法调用的类型和成员,因为它们使用以下声明性安全特性修饰:The following is a list of types and members that are not callable by partially trusted code because they are decorated with the following declarative security attribute:

PermissionSetAttribute(SecurityAction.LinkDemand, Name := "FullTrust")








System.Transactions.TransactionScope.TransactionScope (System.Transactions.Transaction,System.TimeSpan,System.Transactions.EnterpriseServicesInteropOption)System.Transactions.TransactionScope.TransactionScope (System.Transactions.Transaction,System.TimeSpan,System.Transactions.EnterpriseServicesInteropOption)


有关各种信任级别的详细信息,请参阅访问资源中的安全信任级别For more information on the various trust levels, see Security Trust Levels in Accessing Resources.

核心开发 Technologies\Data Access\Transaction 处理 Core Development Technologies\Data Access\Transaction Processing\

有关如何使用 System.Transactions 命名空间的详细信息,可以参阅文档中 "核心开发 Technologies\Data Access\Transaction 处理\" 部分下的概念文档事务处理For more information on how to use the System.Transactions namespace, you can consult the conceptual documentation Transaction Processing, located under the "Core Development Technologies\Data Access\Transaction Processing\" section in the documentation. 具体而言,你可以在以下主题中找到详细信息。Specifically, you can find more information in the following topics.

由系统提供的功能Features Provided By System.Transactions

实现资源管理器Implementing A Resource Manager