/// <summary> /// Initializes a new instance of the <see cref="RMQConfigurationManager"/> class. /// </summary> /// <param name="settings">The settings.</param> public RMQConfigurationManager(RMQConfigSettings settings) { Settings = settings; foreach (var connection in Settings.Connections) { connection.Endpoints.Clear(); foreach (var uri in connection.Uri.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { connection.Endpoints.Add(uri); } } }
/// <summary> /// Initializes a new instance of the <see cref="RMQConfigurationManager"/> class. /// </summary> /// <param name="configPath">The configuration path.</param> public RMQConfigurationManager(string configPath = null) { Settings = new RMQConfigSettings(); if (configPath == null) { var xmlFile = $"{AppDomain.CurrentDomain.BaseDirectory}RMQSettings.config"; var jsonFile = $"{AppDomain.CurrentDomain.BaseDirectory}RMQSettings.json"; if (File.Exists(xmlFile)) { configPath = xmlFile; } else if (File.Exists(jsonFile)) { configPath = jsonFile; } } if (configPath != null) { if (Path.GetExtension(configPath).ToLower() == ".json") { var json = File.ReadAllText(configPath); Settings = JsonConvert.DeserializeObject <RMQConfigSettings>(json); } else // XML legacy { using (var configContainer = new ConfigContainer(configPath, "./rmqSettings")) { var configNode = configContainer.Node; var settingNodes = configNode.GetConfigNode($"./connectionSettings"); if (settingNodes != null) { foreach (var settingNode in settingNodes.GetConfigNodes(".")) { var connectionSettings = new RMQConnectionSettings(); Settings.Connections.Add(connectionSettings); connectionSettings.Name = settingNode.GetAttribute(".", "name", ""); connectionSettings.Username = settingNode.GetAttribute(".", "username", ""); connectionSettings.Password = settingNode.GetAttribute(".", "password", ""); connectionSettings.VirtualHost = settingNode.GetAttribute(".", "virtualhost", "/"); connectionSettings.Uri = settingNode.GetAttribute(".", "uri", "amqp://*****:*****@name='{exchangeSettings.Name}']/arguments"); if (argsNode != null) { var attributes = argsNode.GetAttributes(); foreach (var key in attributes.AllKeys) { exchangeSettings.Arguments[key] = attributes[key]; } } } } var queueNodes = configNode.GetConfigNode($"./queue"); if (queueNodes != null) { foreach (var queueNode in queueNodes.GetConfigNodes(".")) { var queueSettings = new RMQQueueSettings(queueNode.GetAttribute(".", "name", "")); Settings.Queues.Add(queueSettings); queueSettings.QueueName = queueNode.GetAttribute(".", "queueName", ""); queueSettings.Durable = queueNode.GetAttribute(".", "durable", "true") == "true"; queueSettings.Exclusive = queueNode.GetAttribute(".", "exclusive", "false") == "true"; queueSettings.AutoDelete = queueNode.GetAttribute(".", "autoDelete", "false") == "true"; queueSettings.Persistent = queueNode.GetAttribute(".", "persistent", "true") == "true"; var routingKeys = queueNode.GetAttribute(".", "routingKeys", ""); foreach (var key in routingKeys.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { queueSettings.RoutingKeys.Add(key); } var argsNode = configNode.GetConfigNode($"./queue[@name='{queueSettings.Name}']/arguments"); if (argsNode != null) { var attributes = argsNode.GetAttributes(); foreach (var key in attributes.AllKeys) { queueSettings.Arguments[key] = attributes[key]; } } // get consumer info if it exists var consumerNode = configNode.GetConfigNode($"./queue[@name='{queueSettings.Name}']/consumer"); if (consumerNode != null) { queueSettings.ConsumerSettings.Tag = queueNode.GetAttribute(".", "tag", Guid.NewGuid().ToString()); queueSettings.ConsumerSettings.Exclusive = queueNode.GetAttribute(".", "exclusive", "false") == "true"; queueSettings.ConsumerSettings.NoAck = queueNode.GetAttribute(".", "noAck", "true") == "true"; queueSettings.ConsumerSettings.NoLocal = queueNode.GetAttribute(".", "noLocal", "true") == "true"; } } } } } } foreach (var connection in Settings.Connections) { foreach (var uri in connection.Uri.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { connection.Endpoints.Add(uri); } } }