using System; using System.Collections.Generic; using System.Linq; using System.Text; using RabbitMQ.Client; using System.Configuration; using RabbitMQ.Client.Content; namespace RabbitMQ.BLL { public class RabbitmqWrapper { string rabbitmqHost = string.Empty; public RabbitmqWrapper() { rabbitmqHost = ConfigurationManager.AppSettings["RabbitMQHost"]; } public void ExchangeDeclare(string exchangeName, string exchangeType, bool passive, bool durable, bool autoDelte, bool @internal, bool nowait, System.Collections.IDictionary arguments) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { using (IModel ch = conn.CreateModel()) { ch.ExchangeDeclare(exchangeName, exchangeType, passive, durable, autoDelte, @internal, nowait, arguments); } } } public void ExchangeDelete(string exchangeName,bool ifUnused,bool ifEmpty) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { try { using (IModel ch = conn.CreateModel()) { ch.ExchangeDelete(exchangeName, ifUnused, ifEmpty); } } catch (Exception ex) { throw ex; } } } public void QueueDeclare(string queueName, bool passive, bool durable, bool exclusive, bool autoDelelte, bool nowait, System.Collections.IDictionary arguments) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { try { using (IModel ch = conn.CreateModel()) { string finalName = ch.QueueDeclare(queueName, passive, durable, exclusive, autoDelelte, nowait, arguments); } } catch (Exception ex) { throw ex; } } } public void QueueDelete(string queueName, bool ifUnused, bool ifEmpty, bool nowait) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { try { using (IModel ch = conn.CreateModel()) { ch.QueueDelete(queueName, ifUnused, ifEmpty,nowait); } } catch (Exception ex) { throw ex; } } } public void QueueBind(string queueName, string exchangeName, string returnKey, bool nowait, System.Collections.IDictionary arguments) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { try { using (IModel ch = conn.CreateModel()) { ch.QueueBind(queueName, exchangeName, returnKey, nowait, arguments); } } catch (Exception ex) { throw ex; } } } public void QueueUnbind(string queueName, string exchangeName, string returnKey, System.Collections.IDictionary arguments) { using (IConnection conn = new ConnectionFactory().CreateConnection(rabbitmqHost)) { try { using (IModel ch = conn.CreateModel()) { ch.QueueUnbind(queueName, exchangeName, returnKey, arguments); } } catch (Exception ex) { throw ex; } } } public void PublishMessage(string exchangeName,string queueName,string returnKey,string xml) { IConnection conn = new ConnectionFactory().CreateConnection(this.rabbitmqHost); using (IModel ch = conn.CreateModel()) { IBasicProperties props = ch.CreateBasicProperties(); props.ContentType = "text/plain"; props.DeliveryMode = 2; IStreamMessageBuilder b = new StreamMessageBuilder(ch); byte[] byptes = Encoding.UTF8.GetBytes(xml); b.WriteBytes(byptes); ch.BasicPublish(exchangeName, returnKey,null,byptes); } } } }