I have a console app that is publishing messages to a RabbitMQ exchange. Is it possible for a subscriber that is built with MassTransit to consume this message?
This is the publisher code:
public virtual void Send(LogEntryMessage message)
{
using (var connection = _factory.CreateConnection())
using (var channel = connection.CreateModel())
{
var props = channel.CreateBasicProperties();
props.CorrelationId = Guid.NewGuid().ToString();
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
channel.BasicPublish(exchange: _endpointConfiguration.Exchange, routingKey: _endpointConfiguration.RoutingKey, basicProperties: null,
body: body);
}
}
This is the subscriber code:
IBusControl ConfigureBus()
{
return Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username(username);
h.Password(password);
});
cfg.ReceiveEndpoint(host, "LogEntryQueue", e =>
{
e.Handler<LogEntryMessage>(context =>
Console.Out.WriteLineAsync($"Value was entered: {context.Message.MessageBody}"));
});
});
}
This is the consumer code:
public class LogEntryMessageProcessor : IConsumer<LogEntryMessage>
{
public Task Consume(ConsumeContext<LogEntryMessage> context)
{
Console.Out.WriteLineAsync($"Value was entered:
{context.Message.Message.MessageBody}");
return Task.FromResult(0);
}
}