public static List <IRabbitMQRecieverService> LoadDll() { AttachServices = new List <IRabbitMQRecieverService>(); List <IRabbitMQRecieverService> result = new List <IRabbitMQRecieverService>(); string[] files = Directory.GetFiles(Attach, "*.dll"); foreach (var file in files) { Assembly assembly = Assembly.LoadFile(file); if (assembly == null) { continue; } var serviceTypes = assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IRabbitMQRecieverService))); if (serviceTypes != null && serviceTypes.Count() > 0) { foreach (var type in serviceTypes) { IRabbitMQRecieverService rabbitMQRecieverService = Activator.CreateInstance(type) as IRabbitMQRecieverService; if (rabbitMQRecieverService != null && !result.Exists(w => w.GetType() == rabbitMQRecieverService.GetType())) { result.Add(rabbitMQRecieverService); } } } } AttachServices = result; return(result); }
private static void BindConsumer(List <RabbitMQInstance> rabbitMQInstances) { if (rabbitMQInstances == null || rabbitMQInstances.Count == 0) { return; } foreach (var rabbitMQInstance in rabbitMQInstances) { if (rabbitMQInstance == null) { continue; } var _channel = rabbitMQInstance.Connection.CreateModel(); if (_channel == null || _channel.IsClosed) { InitRabbitMQ(rabbitMQInstance); } var consumer = new EventingBasicConsumer(_channel); consumer.Received += (ch, ea) => { var msg = System.Text.Encoding.UTF8.GetString(ea.Body); if (msg == null) { return; } if (string.IsNullOrEmpty(msg)) { return; } string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); Logger.Info("HandleMessage:" + dateTime); try { while (DynamicLoader.IsLoading) { Thread.Sleep(ConstValues.ReLoadSettingFileSleepTime); } IRabbitMQRecieverService rabbitMQRecieverServiceImpl = AssemblyLoader.AttachServices.FirstOrDefault(w => w.GetType().AssemblyQualifiedName == rabbitMQInstance.RabbitMQModel?.ConsumerType); if (rabbitMQRecieverServiceImpl == null) { rabbitMQRecieverServiceImpl = Activator.CreateInstance(Type.GetType(rabbitMQInstance.RabbitMQModel?.ConsumerType)) as IRabbitMQRecieverService; } rabbitMQRecieverServiceImpl?.HandleMessage(ch, ea, msg); } catch (Exception ex) { Logger.Error(ex.ToString()); Logger.Error("ContentErrorStart:" + dateTime + ":" + msg); Logger.Error("ContentErrorFinished:" + dateTime); RabbitMQSender.PublishToAdmin(rabbitMQInstance.RabbitMQModel?.PublishName, msg); } Logger.Info("Current Dealt:" + dateTime); _channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);//手动应答 }; consumer.Shutdown += OnConsumerShutdown; consumer.Registered += OnConsumerRegistered; consumer.Unregistered += OnConsumerUnregistered; consumer.ConsumerCancelled += OnConsumerConsumerCancelled; _channel.BasicConsume(queue: rabbitMQInstance.RabbitMQModel?.Queue, autoAck: false, consumer: consumer); //手动应答 } }