Method WithTransactionAsync
WithTransactionAsync<TResult>(Func<IClientSessionHandle, CancellationToken, Task<TResult>>, TransactionOptions, CancellationToken)
Executes a callback within a transaction, with retries if needed.
Task<TResult> WithTransactionAsync<TResult>(Func<IClientSessionHandle, CancellationToken, Task<TResult>> callbackAsync, TransactionOptions transactionOptions = null, CancellationToken cancellationToken = default)
Parameters
callbackAsync
Func<IClientSessionHandle, CancellationToken, Task<TResult>>The user defined callback.
transactionOptions
TransactionOptionsThe transaction options.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<TResult>
The callback result.
Type Parameters
TResult
The type of callback result.
Remarks
If a command inside the callback fails, it may cause the transaction on the server to be aborted. This situation is normally handled transparently by the driver. However, if the application does not return that error from the callback, the driver will not be able to determine whether the transaction was aborted or not. The driver will then retry the callback indefinitely. To avoid this situation, the application MUST NOT silently handle errors within the callback. If the application needs to handle errors within the callback, it MUST return them after doing so.