Files
tnb.server/apihost/Tnb.API.Entry/Extensions/ConfigureEventBusExtensions.cs
2023-11-06 19:35:59 +08:00

68 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using JNPF;
using JNPF.Common.Core;
using JNPF.EventHandler;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// OSS服务配置拓展.
/// </summary>
public static class ConfigureEventBusExtensions
{
/// <summary>
/// OSS服务配置.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection ConfigureEventBus(this IServiceCollection services)
{
// 注册EventBus服务
// 注册EventBus服务
services.AddEventBus(options =>
{
var config = App.GetOptions<EventBusOptions>();
if (config.EventBusType != EventBusType.Memory)
{
switch (config.EventBusType)
{
case EventBusType.RabbitMQ:
// 创建连接工厂
var factory = new RabbitMQ.Client.ConnectionFactory
{
// 设置主机名
HostName = config.HostName,
// 用户名
UserName = config.UserName,
// 密码
Password = config.Password,
};
// 创建默认内存通道事件源对象可自定义队列路由key比如这里是 eventbus
var rbmqEventSourceStorer = new RabbitMQEventSourceStorer(factory, "eventbus", 3000);
// 替换默认事件总线存储器
options.ReplaceStorer(serviceProvider =>
{
return rbmqEventSourceStorer;
});
break;
}
}
options.UseUtcTimestamp = false;
// 不启用事件日志
options.LogEnabled = false;
// 事件执行器(失败重试)
options.AddExecutor<RetryEventHandlerExecutor>();
});
services.AddConfigurableOptions<EventBusOptions>();
return services;
}
}