private void Bind(IBasicProperties basicProperties, RabbitMqMessage message) { // Unconditionally set boolean properties, because they are always sent. basicProperties.Persistent = message.Persistent; if (message.AppId != null) { basicProperties.AppId = message.AppId; } if (message.ClusterId != null) { basicProperties.ClusterId = message.ClusterId; } if (message.ContentEncoding != null) { basicProperties.ContentEncoding = message.ContentEncoding; } if (message.ContentType != null) { basicProperties.ContentType = message.ContentType; } if (message.CorrelationId != null) { basicProperties.CorrelationId = message.CorrelationId; } if (message.Expiration != null) { basicProperties.Expiration = message.Expiration; } if (message.Headers != null) { basicProperties.Headers = message.Headers; } if (message.MessageId != null) { basicProperties.MessageId = message.MessageId; } if (message.Priority != null) { basicProperties.Priority = message.Priority.Value; } if (message.ReplyTo != null) { basicProperties.ReplyTo = message.ReplyTo; } if (message.Timestamp != null) { basicProperties.Timestamp = new AmqpTimestamp(message.Timestamp.Value); } if (message.Type != null) { basicProperties.Type = message.Type; } if (message.UserId != null) { basicProperties.UserId = message.UserId; } }
private Task SendImpl(RabbitMqMessage message) { var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); try { if (_connection.Logger.IsEnabled(LogLevel.Debug)) { _connection.Logger.LogDebug($"RabbitMqProducer: calling Send."); } var channel = _connection.Channel; if (channel == null) { throw new ObjectDisposedException(nameof(RabbitMqProducer)); } var basicProperties = channel.CreateBasicProperties(); Bind(basicProperties, message); basicProperties.MessageId = Guid.NewGuid().ToString(); _declarationHelper.DeclareExchange(message.Exchange, channel); if (message.ShouldConfirmPublish) { var seqNo = channel.NextPublishSeqNo; var mif = new MessageInFlight(tcs, channel, this); this.AddMessageInFlight(mif, TimeSpan.FromSeconds(10)); } channel.BasicPublish(message.Exchange, message.RoutingKey, true, basicProperties, message.Body); if (!message.ShouldConfirmPublish) { return(Task.CompletedTask); } } catch (Exception ex) { tcs.SetException(new RabbitMqException("RabbitMqProducer: Send failed!", ex)); } return(tcs.Task); }
public Task SendAsync(RabbitMqMessage message) { return(_connection.RunOnScheduler(() => SendImpl(message))); }