/// <summary> /// Publish object as JSON message content /// </summary> public void Publish(object message, string routingKey = null) { string messageStr = JsonConvert.SerializeObject(message); var messageBin = Encoding.UTF8.GetBytes(messageStr); _channelProvider.Provide().BasicPublish( exchange: Name, routingKey: routingKey ?? "", body: messageBin); }
void PublishCore <T>(OutgoingMqEnvelop <T> envelop, PublishTarget pubTarget) where T : class { if (envelop == null) { throw new ArgumentNullException(nameof(envelop)); } if (string.IsNullOrEmpty(pubTarget.Routing) && string.IsNullOrEmpty(pubTarget.Exchange)) { throw new InvalidOperationException($"Publishing target not defined. Payload type '{typeof(T).FullName}'"); } var channel = _channelProvider.Provide(); var basicProperties = CreateBasicProperties <T>(envelop, channel); var payloadStr = JsonConvert.SerializeObject(envelop.Message.Payload, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var payloadBin = Encoding.UTF8.GetBytes(payloadStr); channel.BasicPublish( pubTarget.Exchange, pubTarget.Routing, basicProperties, payloadBin ); }
/// <summary> /// Creates exchange with name = {name} /// </summary> public MqExchange CreateWithName(string name) { var channel = _channelProvider.Provide(); channel.ExchangeDeclare(name, ExchangeType.ToLiteral(), Durable, AutoDelete, null); return(new MqExchange(name, _channelProvider)); }