From 7693c72802bfba49dcc331a6fd56315bc814953c Mon Sep 17 00:00:00 2001 From: hang <87227557@qq.com> Date: Sat, 28 Sep 2024 21:21:52 +0800 Subject: [PATCH] =?UTF-8?q?MasstransitMeditor=20=E4=B8=8D=E5=9C=A8efcore?= =?UTF-8?q?=20=E5=90=8C=E4=B8=80=E4=B8=AA=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/Progranm.cs | 1 + .../IRaCIS.Core.Application.xml | 6 ++-- .../NeedVerify/AddSubjectTriggerConsumer.cs | 4 +-- IRaCIS.Core.Application/TestService.cs | 13 +++++++++ .../Triggers/SubjectTrigger.cs | 1 - IRaCIS.Core.Domain/BaseModel/IAuditAdd.cs | 5 +--- .../Interceptor/AuditEntityInterceptor.cs | 28 ++++++------------- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index e90cbe6c8..89c40314f 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -151,6 +151,7 @@ builder.Services.AddMediator(cfg => { cfg.AddConsumer(); cfg.AddConsumer(); + cfg.AddConsumer(); }); // 添加 MassTransit 和 InMemory 传输 diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index ed0344dd6..0a897fe0a 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -12658,7 +12658,8 @@ - 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 暂时废弃 + 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 + 因为消费者这里的数据库上下文 和消息发送者上下文不是同一个,相当于两个独立的事务 @@ -12667,7 +12668,8 @@ - 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 暂时废弃 + 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 + 因为消费者这里的数据库上下文 和消息发送者上下文不是同一个,相当于两个独立的事务 diff --git a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddSubjectTriggerConsumer.cs b/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddSubjectTriggerConsumer.cs index 3e9ef7abc..3a2e67594 100644 --- a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddSubjectTriggerConsumer.cs +++ b/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddSubjectTriggerConsumer.cs @@ -7,7 +7,8 @@ using MassTransit; namespace IRaCIS.Core.Application.MassTransit.Consumer; /// -/// 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 暂时废弃 +/// 添加Subject 触发添加访视 不能代替 Trigger,稽查BatchId 不一致 +/// 因为消费者这里的数据库上下文 和消息发送者上下文不是同一个,相当于两个独立的事务 /// /// /// @@ -41,6 +42,5 @@ public class AddSubjectTriggerConsumer(IRepository _subjectVisitRe }); await _subjectVisitRepository.AddRangeAsync(svList); - await _subjectVisitRepository.SaveChangesAsync(); } } diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 0c91dbb43..65c5d8134 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -4,11 +4,13 @@ using IRaCIS.Core.Application.BusinessFilter; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.ViewModel; +using IRaCIS.Core.Domain; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Encryption; using IRaCIS.Core.Infrastructure.NewtonsoftJson; using MassTransit; +using MassTransit.Mediator; using Medallion.Threading; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; @@ -119,6 +121,17 @@ namespace IRaCIS.Core.Application.Service } + public async Task TestMasstransitMeditor([FromServices] IMediator _mediator, [FromServices] IRepository _testLengthRepository) + { + await _testLengthRepository.AddAsync(new TestLength() { Name = "zhouhang1" }); + + await _mediator.Send(new AddSubjectTriggerCommand { SubjectId = Guid.Empty }); + + await _testLengthRepository.SaveChangesAsync(); + + return ResponseOutput.Ok(); + } + public async Task TestJson() { var model1 = new TestModel() { TestId = NewId.NextSequentialGuid(), TestName = null }; diff --git a/IRaCIS.Core.Application/Triggers/SubjectTrigger.cs b/IRaCIS.Core.Application/Triggers/SubjectTrigger.cs index 0f936accf..3cddab770 100644 --- a/IRaCIS.Core.Application/Triggers/SubjectTrigger.cs +++ b/IRaCIS.Core.Application/Triggers/SubjectTrigger.cs @@ -117,7 +117,6 @@ namespace IRaCIS.Core.Application.Triggers await _subjectVisitRepository.AddRangeAsync(svList); - //await _subjectVisitRepository.SaveChangesAsync(); } diff --git a/IRaCIS.Core.Domain/BaseModel/IAuditAdd.cs b/IRaCIS.Core.Domain/BaseModel/IAuditAdd.cs index ff8da5068..27c54d5ac 100644 --- a/IRaCIS.Core.Domain/BaseModel/IAuditAdd.cs +++ b/IRaCIS.Core.Domain/BaseModel/IAuditAdd.cs @@ -11,7 +11,4 @@ public interface IAuditAdd : IAuditAdd } -public interface IAuditAddWithUserName : IAuditAdd -{ - public string CreateUser { get; set; } -} + diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs index 750e96ae8..a97b5a65d 100644 --- a/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs +++ b/IRaCIS.Core.Infra.EFCore/Interceptor/AuditEntityInterceptor.cs @@ -12,7 +12,7 @@ using System.Data; namespace IRaCIS.Core.Infra.EFCore; -public class AuditEntityInterceptor(IUserInfo _userInfo, +public class AuditEntityInterceptor(IUserInfo _userInfo, ILogger _logger , IMediator _mediator ) : SaveChangesInterceptor @@ -28,9 +28,9 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, public override ValueTask> SavingChangesAsync(DbContextEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { - ////领域命令 (同一个事务提交的一些逻辑,类似Trigger 保存事务之前执行的一些逻辑) + //////领域命令 (同一个事务提交的一些逻辑,类似Trigger 保存事务之前执行的一些逻辑) //eventData.Context.AddDomainCommands(); - //DispatchDomainCommands(eventData.Context).GetAwaiter().GetResult(); + //await DispatchDomainCommands(eventData.Context); //领域事件 eventData.Context.AddDomainEvents(); @@ -41,6 +41,8 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, //IRC稽查 放在savechange 之前 不影响之前的逻辑 IRCDataInspection(eventData.Context); + //return result; + return base.SavingChangesAsync(eventData, result, cancellationToken); } public override InterceptionResult SavingChanges(DbContextEventData eventData, InterceptionResult result) @@ -55,6 +57,8 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, //IRC稽查 放在savechange 之前 不影响之前的逻辑 IRCDataInspection(eventData.Context); + //return result; + return base.SavingChanges(eventData, result); } public void AuditEntities(DbContext? context) @@ -122,22 +126,6 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, updateEntity.UpdateTime = DateTime.Now; updateEntity.UpdateUserId = _userInfo.Id; } - - if (entry.Entity is IAuditAddWithUserName addEntity3) - { - if (addEntity3.CreateTime == default(DateTime)) - { - addEntity3.CreateTime = DateTime.Now; - - } - if (addEntity3.CreateUserId == default(Guid)) - { - addEntity3.CreateUserId = _userInfo.Id; - addEntity3.CreateUser = _userInfo.RealName; - } - - - } break; } } @@ -164,7 +152,6 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, #region 异常处理 - #endregion private void LoggerDBContextException(DbContextErrorEventData eventData) { @@ -225,6 +212,7 @@ public class AuditEntityInterceptor(IUserInfo _userInfo, base.SaveChangesFailed(eventData); } + #endregion #region 领域命令分发