Share via


ServiceModel Transaction Attributes

Windows Communication Foundation (WCF) provides properties on three standard System.ServiceModel attributes that enable you to configure the behavior of transactions for a WCF service:

TransactionFlowAttribute

The TransactionFlowAttribute attribute specifies the willingness of an operation in a service contract to accept incoming transactions from a client. The attribute provides this control with the following property: Transactions use the TransactionFlowOption enumeration to specify whether an incoming transaction is Mandatory, Allowed, or NotAllowed.

This is the only attribute that relates service operations to external interactions with a client. The attributes described in the following sections relate to the use of transactions within the execution of the operation.

ServiceBehaviorAttribute

The ServiceBehaviorAttribute attribute specifies the internal execution behavior of a service contract implementation. Transaction-specific properties of this attribute include:

  • TransactionAutoCompleteOnSessionClose specifies whether to complete an uncompleted transaction when the session closes. The default value for this property is false. If this property is true, and the incoming session was gracefully shut down instead of closing due to network or client faults, any uncompleted transaction is successfully completed. Otherwise, if this property is false or if the session was not gracefully closed, any uncompleted transaction is rolled back when the session closes. If this property is true, the incoming channel must be session-based.

  • ReleaseServiceInstanceOnTransactionComplete specifies whether the underlying service instance is released when a transaction completes. The default value for this property is true. The next inbound message causes a new underlying instance to be created, discarding any per-transaction state that the previous instance might have held. Releasing a service instance is an internal action the service takes and has no impact on any existing connections or sessions that clients might have established. This functionality is equivalent to the just-in-time activation feature COM+ provides. If the property is true, ConcurrencyMode must be equal to Single. Otherwise, the service throws an invalid configuration validation exception during startup.

  • TransactionIsolationLevel specifies the isolation level to use for transactions within the service; this property takes one of the IsolationLevel values. If the local isolation level property is anything other than Unspecified, the isolation level of an incoming transaction must match the setting of this local property. Otherwise, the incoming transaction is rejected, and a fault is sent back to the client. If TransactionScopeRequired is true, and no transaction is flowed, this property determines the IsolationLevel value to use for the locally created transaction. If IsolationLevel is set to Unspecified, IsolationLevel Serializable is used.

  • TransactionTimeout specifies the time period within which a new transaction created at the service must complete. If this time is reached and the transaction has not been completed, it will abort. The TimeSpan is used as the TransactionScope time-out for any operations that have TransactionScopeRequired set to true and for which a new transaction was created. The time-out is the maximum allowed duration from the creation of the transaction to the completion of phase 1 in the two-phase commit protocol. The time-out value used is always the lower value between the TransactionTimeout property and the transactionTimeout configuration setting.

OperationBehaviorAttribute

The OperationBehaviorAttribute attribute specifies the behaviors of the methods in the service implementation. You can use it to indicate the operation's specific execution behavior. Properties of this attribute do not affect the Web Service Description Language (WSDL) description of the service contract, and are purely elements of the WCF programming model that enable common features that developers otherwise have to implement themselves.

This attribute has the following transaction-specific properties:

  • TransactionScopeRequired specifies whether a method must execute within an active transaction scope. The default is false. If the OperationBehaviorAttribute attribute is not set for a method, it also implies that the method will not execute in a transaction. If a transaction scope is not required for an operation, any transaction that is present within the message header is not activated and remains as an element of the IncomingMessageProperties of the OperationContext. If a transaction scope is required for an operation, the source for the transaction is derived from one of the following:

    • If a transaction is flowed from the client, the method is executed under a transaction scope created using that distributed transaction.

    • With a queued transport, the transaction used to dequeue the message is used. Note that the transaction used is not a flowed transaction, in that it was not provided by the original sender of the message.

    • A custom transport can provide a transaction through the use of the TransportTransactionProperty.

    • If none of the above provides an external source for a transaction, a new Transaction instance is created immediately prior to calling the method.

  • TransactionAutoComplete specifies whether the transaction in which the method executes is automatically completed if no unhandled exceptions are thrown. If this property is true, the calling infrastructure automatically marks the transaction as "completed" if the user method returns without throwing an exception. If this property is false, the transaction is attached to the instance, and is only marked as "completed" if the client calls a subsequent method that is marked with this property equal to true, or if a subsequent method explicitly calls SetTransactionComplete. Failure to perform either of these results in the transaction never being "completed," and the contained work is not committed, unless the TransactionAutoCompleteOnSessionClose property is set to true. If this property is set to true, you must use a channel with a session, and the InstanceContextMode must be set to PerSession.