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. 若要开始,请参阅实现隐式 Transaction Using Transaction Scope主题。To get started, see the Implementing An Implicit Transaction Using Transaction Scope topic. 编写事务应用程序的详细信息,请参阅Writing A Transactional ApplicationFor 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. 有关实现资源管理器的详细信息,请参阅Implementing A Resource ManagerFor 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. 中的深度中介绍了这一领域Transaction Management Escalation主题。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. 中的深度中介绍了这一领域优化使用单个 Phase Commit and Promotable Single Phase Notification主题。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.Transactions程序集可以由部分受信任代码调用,因为它已标记有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.

核心开发技术 Access\Transaction Processing\Core Development Technologies\Data Access\Transaction Processing\

有关如何使用的详细信息System.Transactions命名空间,可以参考的相关概念性文档事务处理下的"核心开发技术 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.

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

实现资源管理器Implementing A Resource Manager