diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index eafdfa1cd..b3a34ede8 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 static IRaCIS.Core.Application.Service.TestService; AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); @@ -163,14 +164,33 @@ builder.Services.AddMassTransit(cfg => cfg.AddConsumer(); // 替换为你的消费者类 cfg.AddConsumer(); + cfg.AddConsumer(); + + + cfg.AddPublishMessageScheduler(); + //cfg.AddHangfireConsumers(); + + + // 使用 InMemory 作为消息传递机制 cfg.UsingInMemory((context, cfg) => { + //https://github.com/MassTransit/Sample-Hangfire/blob/master/src/Sample.Hangfire.Console/Program.cs + cfg.UsePublishMessageScheduler(); + + //使用 Hangfire 进行消息调度 + cfg.UseHangfireScheduler(); + // 这里可以进行额外的配置 cfg.ConfigureEndpoints(context); // 自动配置所有消费者的端点 + }); }); +//builder.Services.AddOptions().Configure(options => +//{ +// options.WaitUntilStarted = true; +//}); #endregion diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 6127e3522..50aa9433d 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -162,6 +162,27 @@ namespace IRaCIS.Core.Application.Service return ResponseOutput.Ok(); } + public class MasstransitHangfireTest + { + public string value { get; set; } + } + + public class MasstransitHangfireTestConsumer : IConsumer + { + public Task Consume(ConsumeContext context) + { + Console.WriteLine("Now is " + DateTime.Now.ToString()); + Console.WriteLine($"MassTransit.Consumer1 :{context.Message.value}"); + return Task.CompletedTask; + } + } + public async Task TestMasstransitHangfire([FromServices] IMessageScheduler _scheduler) + { + await _scheduler.SchedulePublish(DateTime.Now.AddSeconds(10), new MasstransitHangfireTest() { value = "message at " + DateTime.Now.ToString() }); + + return ResponseOutput.Ok(); + } + public async Task TestJson() { var model1 = new TestModel() { TestId = NewId.NextSequentialGuid(), TestName = null }; diff --git a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj index aac993e26..684798ebc 100644 --- a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj +++ b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj @@ -27,6 +27,7 @@ +