Exceptions in Transactions

I have a service operation that throws an exception to return an error back to the client. When the service operation is transactional, the transaction is aborted after calling this operation. How do I return the error under the transaction?

A service operation call fails when the invoked method completes abnormally due to an unhandled exception. The service can choose to handle an operation failure in a graceful way by applying an error handling policy. A common error handling policy is to convert the unhandled exception into a response message that is sent back to the client. The service operation still fails, but the failure proceeds such that the behavior is anticipated and allowed by the contract of the web service, so both the client and server can recover from the error and continue executing. This allows the client to handle the error condition mostly the same as a successful service operation call.

However, the service operation really did fail due to the unhandled exception. A transaction has a standard behavior when the invoked method completes abnormally, which is to abort. Although the error handling policy of the service has done something graceful about substituting an error response message for the application response, rolling back is the graceful error behavior of a transaction.

You can craft the corresponding error response message yourself and send it if you want the effect of a failed message exchange without a failed transaction. This behavior would be confusing though because the client probably expects to use the application response in the next step of the transacted work. While you may have averted the transaction rollback for now, there's no guarantee that the client can make any useful progress.