Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 25, 2025 19:48
Show Gist options
  • Save dcomartin/8207eef9eb941c118176f3988b4cf599 to your computer and use it in GitHub Desktop.
Save dcomartin/8207eef9eb941c118176f3988b4cf599 to your computer and use it in GitHub Desktop.
public class MessageSessionFilter : IAsyncResourceFilter
{
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
if (context.ActionDescriptor.Parameters.Any(p => p.ParameterType == typeof(ITransactionalSession)))
{
var session = context.HttpContext.RequestServices.GetRequiredService<ITransactionalSession>();
await session.Open(new SqlPersistenceOpenSessionOptions());
var result = await next();
if (result.Exception is null)
{
await session.Commit();
}
}
else
{
await next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment