using IRaCIS.Core.API.HostService; using IRaCIS.Core.Application.MassTransit.Consumer; using IRaCIS.Core.Domain.BaseModel; using IRaCIS.Core.Infra.EFCore; using MassTransit; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; namespace IRaCIS.Core.API { public static class MassTransitSetup { public static void AddMassTransitSetup(this IServiceCollection services) { #region MassTransit //masstransit组件 也支持MediatR 中介者模式,但是支持分布式,考虑后续,所以在次替代MediatR //参考链接:https://masstransit.io/documentation/concepts/mediator#scoped-mediator services.AddMediator(cfg => { cfg.AddConsumers(typeof(UserSiteSurveySubmitedEventConsumer).Assembly); //cfg.AddConsumer(); //cfg.AddConsumer(); //cfg.AddConsumer(); //cfg.ConfigureMediator((context, cfg) => cfg.UseHttpContextScopeFilter(context)); }); //添加 MassTransit 和 InMemory 传输 services.AddMassTransit(cfg => { cfg.AddConsumers(typeof(UserSiteSurveySubmitedEventConsumer).Assembly); cfg.AddPublishMessageScheduler(); cfg.AddHangfireConsumers(); // 使用 InMemory 作为消息传递机制 cfg.UsingInMemory((context, cfg) => { cfg.UsePublishMessageScheduler(); cfg.UseConsumeFilter(typeof(ConsumeExceptionFilter<>), context, x => x.Include(type => type.IsAssignableTo(typeof(DomainEvent)))); cfg.UseConsumeFilter(typeof(CultureInfoFilter<>), context, x => x.Include(type => type.IsAssignableTo(typeof(DomainEvent)))); cfg.ConfigureEndpoints(context); // 自动配置所有消费者的端点 }); #region rabitmq obsolute //cfg.UsingRabbitMq((context, cfg) => //{ // cfg.UsePublishMessageScheduler(); // cfg.Host( // host: "106.14.89.110", // port: 5672, // virtualHost: "/", // configure: hostConfig => // { // hostConfig.Username("rabbitmq"); // hostConfig.Password("rabbitmq"); // }); // cfg.ConfigureEndpoints(context); //}); #endregion #region Outbox obsolute //cfg.AddConfigureEndpointsCallback((context, name, cfg) => //{ // cfg.UseEntityFrameworkOutbox(context); // //cfg.UseDelayedRedelivery(r => r.Intervals(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(30))); // //// 全局重试策略:重试 3 次,每次延迟 5 秒 // //cfg.UseMessageRetry(retryConfig => // //{ // // retryConfig.Interval(3, TimeSpan.FromSeconds(10)); // //}); //}); //cfg.AddEntityFrameworkOutbox(o => //{ // o.UseSqlServer(); // o.UseBusOutbox(); //}); #endregion }); //services.AddOptions() // .Configure(options => // { // options.WaitUntilStarted = true; // options.StartTimeout = TimeSpan.FromMinutes(1); // options.StopTimeout = TimeSpan.FromMinutes(1); // }); //services.AddOptions() // .Configure(options => options.ShutdownTimeout = TimeSpan.FromMinutes(1)); //services.AddHostedService(); #endregion } } }