private void ReceiveMessage(RabbitMQConnectionDTO connectionDTO, string exchangeName, string queueName, string routtingKey) { try { var factory = new ConnectionFactory { HostName = connectionDTO.HostName, Password = connectionDTO.Password, UserName = connectionDTO.UserName, Port = connectionDTO.Port, }; UseDefaultBasicConsumerType(factory, queueName); //DirectAcceptExchangeEvent(factory, exchangeName, queueName, routtingKey); } catch (Exception ex) { Rtx_SendContext.Text = $"出现异常:{ex.Message}"; } }
private void Btn_Begin_Click(object sender, EventArgs e) { Rtx_SendContext.Text = $"开始监听MQ!"; Btn_Begin.Enabled = false; _connectionDTO = new RabbitMQConnectionDTO(); string exchangeName, queueName, routtingKeyName; string iniPath = Path.Combine(Application.StartupPath, "AppSetting.ini"); _connectionDTO.HostName = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Connection", "HostName", ""); _connectionDTO.Password = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Connection", "Password", ""); _connectionDTO.UserName = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Connection", "UserName", ""); string portIniValue = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Connection", "Port", ""); if (string.IsNullOrWhiteSpace(_connectionDTO.HostName)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Connection]section下的HostName节点为空!"); return; } if (string.IsNullOrWhiteSpace(_connectionDTO.Password)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Connection]section下的Password节点为空!"); return; } if (string.IsNullOrWhiteSpace(_connectionDTO.UserName)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Connection]section下的UserName节点为空!"); return; } int port; if (!int.TryParse(portIniValue, out port)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Connection]section下的Port节点为空或格式有误!"); return; } else { _connectionDTO.Port = port; } exchangeName = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Queue", "ExchangeName", ""); queueName = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Queue", "QueueName", ""); routtingKeyName = IniHelper.INIGetStringValue(iniPath, "RabbitMQ_Queue", "RoutingKeyName", ""); if (string.IsNullOrWhiteSpace(exchangeName)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Queue]section下的ExchangeName节点为空!"); return; } if (string.IsNullOrWhiteSpace(queueName)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Queue]section下的QueueName节点为空!"); return; } if (string.IsNullOrWhiteSpace(routtingKeyName)) { MessageBox.Show($"AppSetting.ini配置文件--[RabbitMQ_Queue]section下的RoutingKeyName节点为空!"); return; } ReceiveMessage(_connectionDTO, exchangeName, queueName, routtingKeyName); }