public Task PublishVideoEvent(string exchange, string routing, VideoEvent video) { using (var channel = _connection.CreateModel()) { channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Topic, arguments: null); var message = JsonConvert.SerializeObject(video); var body = Encoding.UTF8.GetBytes(message); IBasicProperties properties = channel.CreateBasicProperties(); properties.Persistent = true; properties.DeliveryMode = 2; channel.ConfirmSelect(); channel.BasicPublish(exchange: exchange, routingKey: routing, mandatory: true, basicProperties: properties, body: body); channel.WaitForConfirmsOrDie(); channel.BasicAcks += (sender, eventArgs) => { Console.WriteLine("Sent to Event Bus"); //implement ack handle }; channel.ConfirmSelect(); return(Task.CompletedTask); } }
public void ConsumeAudio(string exchange, string routing, string queue) { var channel = _connection.CreateModel(); channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Topic); channel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind(queue: queue, exchange: exchange, routingKey: $"{routing}.*"); var consumer = new EventingBasicConsumer(channel); consumer.Received += async(sender, e) => { var message = Encoding.UTF8.GetString(e.Body.Span); var audioEvent = JsonConvert.DeserializeObject <AudioEvent>(message); var audio = _mapper.Map <Audio>(audioEvent); var result = await _audioRepository.CreateAudio(audio); }; channel.BasicConsume(queue, true, consumer); }
/// <summary> /// Creates a Topic Client and Publishes IntegrationEvent to the AzureServiceBus /// </summary> /// <param name="event">Message sent from the component</param> public async Task PublishAsync(IntegrationEvent @event) { var eventName = @event.GetType().Name.Replace(INTEGRATION_EVENT_SUFIX, ""); var jsonMessage = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(jsonMessage); var message = new Message { MessageId = Guid.NewGuid().ToString(), Body = body, Label = eventName, }; var topicClient = serviceBus.CreateModel(); await topicClient.SendAsync(message); }