Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created July 10, 2025 21:18
Show Gist options
  • Select an option

  • Save dcomartin/4ecb4c428c500f96c5ee1504d96cbdea to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/4ecb4c428c500f96c5ee1504d96cbdea 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