WebExtensions.UseTransaction(this MvcOptions options, double scopeTimeout = 0, IsolationLevel isolationLevel = IsolationLevel.Serializable, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled, CommitPolicy commitPolicy = CommitPolicy.OnActionExecuted) Method
.NET Standard 2.x
Uses the TransactionScope
instance to automatically manage transactions for all actions in all controllers.
Namespace: SnapObjects.Data.AspNetCore
Assembly: SnapObjects.Data.AspNetCore.dll
Syntax
public static MvcOptions UseTransaction(
this MvcOptions options,
double scopeTimeout = 0,
IsolationLevel isolationLevel = IsolationLevel.Serializable,
TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled,
CommitPolicy commitPolicy = CommitPolicy.OnActionExecuted)
Parameters
options
Microsoft.AspNetCore.Mvc.MvcOptions
The MvcOptions
object which provides programmatic configuration for the MVC framework.
scopeTimeout
System.Double
The number of the millisecond after which the transaction scope times out and aborts the transaction.
Default is 0
.
isolationLevel
System.Transactions.IsolationLevel
An IsolationLevel
enumeration that specifies the isolation level of transaction.
Default is IsolationLevel.Serializable
.
asyncFlowOption
System.Transactions.TransactionScopeAsyncFlowOption
A TransactionScopeAsyncFlowOption
enumeration that describes whether the ambient transaction associated with the transaction scope will flow across thread continuations when using Task or async/await .NET async programming patterns.
Default is TransactionScopeAsyncFlowOption.Enabled
.
commitPolicy
SnapObjects.Data.AspNetCore.CommitPolicy
A CommitPolicy
enumeration that specifies when to commit the transaction.
Default is CommitPolicy.OnActionExecuted
.
Returns
Microsoft.AspNetCore.Mvc.MvcOptions
Returns the MvcOptions
object which provides programmatic configuration for the MVC framework.
Remarks
It creates a TransactionScope
instance to start a transaction before executing each action in the controller, and commit the transaction after executing the action successfully. If an exception has been thrown, the transaction is rolled back.
Examples
The following code demonstrates how to use the UseTransactionScope
method in the ConfigureServices
method of the Startup
class to automatically manage transactions for all actions in all controllers.
using SnapObjects.Data.AspNetCore;
using Microsoft.Extensions.DependencyInjection;
using System.Transactions;
namespace Appeon.ApiDoc.WebExtensionsExamples
{
public class Startup_UseTransactionExample
{
// Uses this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(m =>
{
// Sets the timeout period for the transaction to 1000 ms;
// Sets the isolation level of the transaction to Serializable;
// Sets to suppress the transaction flow across thread continuations;
// Sets to commit the transaction after the action result executes.
m.UseTransaction(
1000,
IsolationLevel.Serializable,
TransactionScopeAsyncFlowOption.Suppress,
CommitPolicy.OnResultExecuted);
});
}
}
}
Applies to
.NET Standard
2.x