🍒 fix(RabbitMq): 修复RabbitMq未启用导致相关组件注入异常问题

This commit is contained in:
喵你个汪呀 2025-08-30 14:47:01 +08:00
parent dcbab6eba3
commit ebfcbd79f8
5 changed files with 5 additions and 10 deletions

View File

@ -6,7 +6,7 @@
namespace Admin.NET.Application; namespace Admin.NET.Application;
public class RabbitMqHandler : IMessageHandler public class RabbitMqHandler : IMessageHandler, ISingleton
{ {
public string QueueName => "admin.net.rabbitmq"; // 队列名称 public string QueueName => "admin.net.rabbitmq"; // 队列名称

View File

@ -19,7 +19,7 @@ public class Startup : AppStartup
var consumerCount = option.ConsumerCount.ToIntOrDefault(1); var consumerCount = option.ConsumerCount.ToIntOrDefault(1);
// 注册RabbitMQ服务 // 注册RabbitMQ服务
services.AddRabbitMQ<RabbitMqHandler>(consumerCount); services.AddRabbitMQ(consumerCount);
// 后台服务异常,不影响主程序(配置的服务器连接异常) // 后台服务异常,不影响主程序(配置的服务器连接异常)
services.Configure<HostOptions>(options => services.Configure<HostOptions>(options =>

View File

@ -14,15 +14,10 @@ public static class RabbitMQSetup
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="consumerCount">消费者数量默认1个</param> /// <param name="consumerCount">消费者数量默认1个</param>
/// <returns></returns> /// <returns></returns>
public static void AddRabbitMQ<THandler>(this IServiceCollection services, int consumerCount = 1) public static void AddRabbitMQ(this IServiceCollection services, int consumerCount = 1)
where THandler : class, IMessageHandler
{ {
if (App.GetConfig<string>("EventBus:EventSourceType", true) != "RabbitMQ") return; if (App.GetConfig<string>("EventBus:EventSourceType", true) != "RabbitMQ") return;
services.AddSingleton<IMessageHandler, THandler>();
services.AddSingleton<RabbitMqConnection>();
services.AddSingleton<RabbitMqProducer>();
// 注册多个消费者服务 // 注册多个消费者服务
for (int i = 0; i < consumerCount; i++) for (int i = 0; i < consumerCount; i++)
{ {

View File

@ -8,7 +8,7 @@ using RabbitMQ.Client;
namespace Admin.NET.Core; namespace Admin.NET.Core;
public class RabbitMqConnection public class RabbitMqConnection : ISingleton
{ {
private IConnection _connection; private IConnection _connection;
private IChannel _channel; private IChannel _channel;

View File

@ -11,7 +11,7 @@ namespace Admin.NET.Core;
/// <summary> /// <summary>
/// 生产者 /// 生产者
/// </summary> /// </summary>
public class RabbitMqProducer public class RabbitMqProducer : ISingleton
{ {
private readonly RabbitMqConnection _connection; private readonly RabbitMqConnection _connection;