public void Send(ISendContext context) { AddProducerBinding(); _connectionHandler.Use(connection => { try { IBasicProperties properties = _producer.CreateProperties(); properties.SetPersistent(context.DeliveryMode == DeliveryMode.Persistent); properties.MessageId = context.MessageId ?? properties.MessageId ?? NewIds.NewId.Next().ToString(); if (context.ExpirationTime.HasValue) { DateTime value = context.ExpirationTime.Value; properties.Expiration = (value.Kind == DateTimeKind.Utc ? value - SystemUtil.UtcNow : value - SystemUtil.Now). TotalMilliseconds.ToString("F0", CultureInfo.InvariantCulture); } using (var body = new MemoryStream()) { context.SerializeTo(body); properties.Headers = context.Headers.ToDictionary(entry => entry.Key, entry => (object)entry.Value); properties.Headers["Content-Type"] = context.ContentType; _producer.Publish(_address.Name, properties, body.ToArray()); _address.LogSent(context.MessageId ?? properties.MessageId ?? "", context.MessageType); } } catch (AlreadyClosedException ex) { throw new InvalidConnectionException(_address.Uri, "Connection was already closed", ex); } catch (EndOfStreamException ex) { throw new InvalidConnectionException(_address.Uri, "Connection was closed", ex); } catch (OperationInterruptedException ex) { throw new InvalidConnectionException(_address.Uri, "Operation was interrupted", ex); } catch (IOException ex) { if (ex.InnerException is SocketException) { throw new InvalidConnectionException(_address.Uri, "Connection to RabbitMQ host failed", ex); } } }); }