diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs index 903e60a16f..4673d8eb62 100644 --- a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpAspNetCoreSignalRModule.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.SignalR.Auditing; using Volo.Abp.AspNetCore.SignalR.Authentication; +using Volo.Abp.AspNetCore.SignalR.Uow; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; @@ -39,6 +40,7 @@ public class AbpAspNetCoreSignalRModule : AbpModule { options.AddFilter(); options.AddFilter(); + options.AddFilter(); options.AddFilter(); }); diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilter.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilter.cs new file mode 100644 index 0000000000..b2f38c692b --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilter.cs @@ -0,0 +1,42 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.Uow; + +namespace Volo.Abp.AspNetCore.SignalR.Uow; + +public class AbpUowHubFilter : IHubFilter +{ + public virtual async ValueTask InvokeMethodAsync(HubInvocationContext invocationContext, Func> next) + { + object result = null; + + var options = await CreateOptionsAsync(invocationContext); + + var unitOfWorkManager = invocationContext.ServiceProvider.GetRequiredService(); + + using (var uow = unitOfWorkManager.Begin(options)) + { + result = await next(invocationContext); + await uow.CompleteAsync(); + } + + return result; + } + + private async Task CreateOptionsAsync(HubInvocationContext invocationContext) + { + var options = new AbpUnitOfWorkOptions(); + + var defaultOptions = invocationContext.ServiceProvider.GetRequiredService>().Value; + var uowHubFilterOptions = invocationContext.ServiceProvider.GetRequiredService>().Value; + options.IsTransactional = defaultOptions.CalculateIsTransactional( + autoValue: invocationContext.ServiceProvider.GetRequiredService().IsTransactional + ?? await uowHubFilterOptions.IsTransactional(invocationContext) + ); + + return options; + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilterOptions.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilterOptions.cs new file mode 100644 index 0000000000..679f341c30 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Uow/AbpUowHubFilterOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; + +namespace Volo.Abp.AspNetCore.SignalR.Uow; + +public class AbpUowHubFilterOptions +{ + public Func> IsTransactional { get; set; } + + public AbpUowHubFilterOptions() + { + IsTransactional = context => Task.FromResult(true); + } +}