/// <summary>
 /// Publish a message.
 /// </summary>
 /// <param name="messageType">Type of the message.</param>
 /// <param name="message">The message to publish.</param>
 /// <param name="routingKey">The routingkey to use (RabbitMQ specific).</param>
 public Task PublishMessageAsync(string messageType, object message, string routingKey)
 {
     return(Task.Run(() =>
                     Policy
                     .Handle <Exception>()
                     .WaitAndRetry(9, r => TimeSpan.FromSeconds(5), (ex, ts) => { Log.Error("Error connecting to RabbitMQ. Retrying in 5 sec."); })
                     .Execute(() =>
     {
         var factory = new ConnectionFactory()
         {
             HostName = _host, UserName = _username, Password = _password
         };
         using (var connection = factory.CreateConnection())
         {
             using (var model = connection.CreateModel())
             {
                 model.ExchangeDeclare(_exchange, "fanout", durable: true, autoDelete: false);
                 string data = MessageSerializer.Serialize(message);
                 var body = Encoding.UTF8.GetBytes(data);
                 IBasicProperties properties = model.CreateBasicProperties();
                 properties.Headers = new Dictionary <string, object> {
                     { "MessageType", messageType }
                 };
                 model.BasicPublish(_exchange, routingKey, properties, body);
             }
         }
     })));
 }
Exemple #2
0
 /// <summary>
 /// Publish a message.
 /// </summary>
 /// <param name="messageType">Type of the message.</param>
 /// <param name="message">The message to publish.</param>
 /// <param name="routingKey">The routingkey to use (RabbitMQ specific).</param>
 public Task PublishMessageAsync(string messageType, object message, string routingKey)
 {
     return(Task.Run(() =>
     {
         string data = MessageSerializer.Serialize(message);
         var body = Encoding.UTF8.GetBytes(data);
         IBasicProperties properties = _model.CreateBasicProperties();
         properties.Headers = new Dictionary <string, object> {
             { "MessageType", messageType }
         };
         _model.BasicPublish(_exchange, routingKey, properties, body);
     }));
 }