diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index c5bcdb419..6c9da888d 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -35,6 +35,7 @@ using IRaCIS.Core.Application.MassTransit.Command; using IRaCIS.Core.Application.MassTransit.Consumer; using DocumentFormat.OpenXml.InkML; using IRaCIS.Core.Domain; +using MassTransit.Internals; AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); @@ -182,6 +183,10 @@ builder.Services.AddMassTransit(cfg => //使用 Hangfire 进行消息调度 cfg.UseHangfireScheduler(); + cfg.UseConsumeFilter(typeof(CultureInfoFilter<>), context, + x => x.Include(type => type.HasInterface(typeof(IConsumer<>)))); + + // 这里可以进行额外的配置 cfg.ConfigureEndpoints(context); // 自动配置所有消费者的端点 diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index a674db29d..7e404b9c7 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -937,6 +937,13 @@ + + + 重新发布失败的事件消息 + + + + ExploreRecommendService diff --git a/IRaCIS.Core.Application/MassTransit/CultureInfoMiddleware.cs b/IRaCIS.Core.Application/MassTransit/CultureInfoMiddleware.cs new file mode 100644 index 000000000..b0f621855 --- /dev/null +++ b/IRaCIS.Core.Application/MassTransit/CultureInfoMiddleware.cs @@ -0,0 +1,34 @@ +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; +using IRaCIS.Core.Domain.BaseModel; +using MassTransit; + +public class CultureInfoFilter : IFilter> where T : DomainEvent +{ + public async Task Send(ConsumeContext context, IPipe> next) + { + var cultureInfoName = context.Message.CultureInfoName; + + if (!string.IsNullOrEmpty(cultureInfoName)) + { + // 设置线程的文化信息 + Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureInfoName); + Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureInfoName); + } + + try + { + // 继续处理消息 + await next.Send(context); + } + finally + { + // 还原文化信息(可选) + // Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; // 或者之前的文化 + // Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; + } + } + + public void Probe(ProbeContext context) { } +} diff --git a/IRaCIS.Core.Application/Service/Common/EventStoreRecordService.cs b/IRaCIS.Core.Application/Service/Common/EventStoreRecordService.cs index 67b9b5cdd..53926043e 100644 --- a/IRaCIS.Core.Application/Service/Common/EventStoreRecordService.cs +++ b/IRaCIS.Core.Application/Service/Common/EventStoreRecordService.cs @@ -15,10 +15,12 @@ using MassTransit.Mediator; using Newtonsoft.Json; using MassTransit; using IRaCIS.Core.Domain; +using IRaCIS.Core.Domain.Share; +using System.Globalization; namespace IRaCIS.Core.Application.Service; [ApiExplorerSettings(GroupName = "Common")] -public class EventStoreRecordService(IRepository _eventStoreRecordRepository, IMediator _mediator) : BaseService +public class EventStoreRecordService(IRepository _eventStoreRecordRepository, IMediator _mediator,IPublishEndpoint _publishEndpoint) : BaseService { [HttpPost] @@ -33,20 +35,26 @@ public class EventStoreRecordService(IRepository _eventStoreRe return pageList; } - + /// + /// 重新发布失败的事件消息 + /// + /// + /// public async Task RePublishEvent(Guid eventId) { var storedEvent = await _eventStoreRecordRepository.FirstOrDefaultAsync(t => t.Id == eventId); var domainEvent = storedEvent.EventData.JsonStrToObject(Type.GetType(storedEvent.EventType)); - Console.WriteLine(Type.GetType(storedEvent.EventType)); - - Console.WriteLine(domainEvent.GetType()); + CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US); - Console.WriteLine(new UserSiteSurveySubmitedEvent().GetType().AssemblyQualifiedName); + var tt = (UserSiteSurveySubmitedEvent)domainEvent; - await _mediator.Publish(domainEvent.GetType(), domainEvent); + tt.CultureInfoName = CultureInfo.CurrentCulture.Name; + + //IMediator 发布的时候,设置当前线程的CultureInfo 消费者会与发布线程CultureInfo 一致 分布式的话 _publishEndpoint 会不一致,需要手动设置 + + await _publishEndpoint.Publish(domainEvent.GetType(), tt); return ResponseOutput.Ok();