Skip to content

Files

Latest commit

e32530e · Feb 23, 2024

History

History

PostSharp.Samples.Transactions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Mar 8, 2017
Mar 8, 2017
Jul 31, 2019
Jun 4, 2019
Nov 6, 2017
Nov 6, 2017
Feb 23, 2024
Jun 4, 2019
Nov 3, 2017
Jun 4, 2019

This example demonstrates how to build an aspect RequiresTransactionAttribute that forces the method to which it is applied to execute into a transaction. The aspect relies on the System.Transactions namespace, which supports distributed transactions, i.e. transactions involving several transactional services.

You can add this aspect to any method that must run in a transaction, even a distibuted one. However, these methods should not have non-transactionalside effects. Most importantly, transactional methods should not make changes to memory that may be visible to other methods or threads.

You can use this aspect both in a desktop and server-side application. However, it is generally not recommended to manage distributed transactions on the client side.

The aspect opens a TransactionScope at the beginning of the target method and calls the TransactionScope.Complete method at the end of the target method. The aspect also adds a try/finally around the target method so it can dispose the TransactionScope before the method exits.

The aspect demonstrates the use of OnMethodBoundaryAspect and MethodExecutionTag. OnMethodBoundaryAspect is responsible for adding the try/catch block and injecting the code, and MethodExecutionTag allows you to store the TransactionScope object between OnEntry, OnSuccess and OnExit.