Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Infrastructure/RabbitMQ/RabbiteMQExtensions.cs
glz e493c85d08 refactor,feat: 批量代码重构与新增业务模块
1. 重命名枚举项与实体字段,修正类型引用
2. 新增IsNull扩展方法与多项字符串处理扩展
3. 新增大量业务DTO、服务接口与枚举定义
4. 重构RabbitMQ服务实现,替换旧版消息队列组件
5. 优化签到服务的宠物喂养事务逻辑
6. 移除冗余的项目引用与旧版消息队列代码
7. 新增Excel导出、导入模板相关工具方法
2026-06-11 17:01:24 +08:00

57 lines
2.5 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 Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;
namespace QYZH.InteractiveMagazine.Infrastructure.RabbitMQ
{
public static class RabbiteMQExtensions
{
/// <summary>
/// 初始化消息队列并添加Publisher到IoC容器
/// </summary>
/// <remarks>从Configuration读取"RabbbitMQOptions配置项"</remarks>
public static IServiceCollection AddRabbitMQ(this IServiceCollection services, IConfiguration configuration)
{
var rabbitMqSection = configuration.GetSection("RabbitMq");
if (rabbitMqSection.Exists())
{
// 绑定RabbitMQ配置
services.Configure<RabbitMQOptions>(rabbitMqSection);
// 注册RabbitMQ连接工厂
services.AddSingleton<IRabbitMQConnection, RabbitMQConnection>(sp =>
{
var options = sp.GetRequiredService<IOptions<RabbitMQOptions>>().Value;
var factory = new ConnectionFactory()
{
HostName = options.HostName,
Port = options.Port,
UserName = options.UserName,
Password = options.Password,
VirtualHost = options.VirtualHost,
// 自动恢复配置
AutomaticRecoveryEnabled = true, // 启用自动恢复
NetworkRecoveryInterval = TimeSpan.FromSeconds(10), // 每10秒尝试重连
// 心跳检测
RequestedHeartbeat = TimeSpan.FromSeconds(10), // 60秒心跳
// 其他重要配置
TopologyRecoveryEnabled = true, // 恢复交换机、队列等拓扑结构
RequestedConnectionTimeout = TimeSpan.FromSeconds(30), // 连接超时
SocketReadTimeout = TimeSpan.FromSeconds(30), // 读取超时
SocketWriteTimeout = TimeSpan.FromSeconds(30) // 写入超时
};
return new RabbitMQConnection(factory);
});
// 添加RabbitMQService的服务注册
services.AddSingleton<IRabbitMQService, RabbitMQService>();
//services.AddHostedService<TerminalReportService>();
}
return services;
}
}
}