I'd like to send a message to an appropriate queue based on the routing key. Starting from the producer app, my code (the relevant parts) is:
var options = serviceProvider
.GetService<IConfiguration>()
.GetOptions<RabbitMqProducerOptions>("RabbitMqProducer");
foreach (var option in options?.Endpoints)
{
var method = typeof(EndpointConvention).GetMethod("Map", new[] { typeof(Uri) });
var type = Assembly.Load(option.Assembly).GetTypes().First(t => t.Name == option.Type);
var genericMethod = method.MakeGenericMethod(new[] { type });
genericMethod.Invoke(null, new[] { new Uri($"{options.Address}/{option.Name}") });
}
Bus.Factory.CreateUsingRabbitMq(cfg => {
cfg.Host(options.Address);
cfg.Send<CreateProducts>(x => x.UseRoutingKeyFormatter(context => context.Message.Platform));
});
The above is ok - it creates exchanges as I declared them in the configuration file (fanout exchanges, if that matters). Now the consumer configuration:
var options = serviceProvider.GetService<IConfiguration>().GetOptions<RabbitMqConsumerOptions>("RabbitMqConsumer");
Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(options.Address);
foreach (var kvp in options.Endpoints)
{
cfg.ReceiveEndpoint("ingest-products", ep =>
{
ep.PrefetchCount = kvp.Value.PrefetchCount;
ep.BindMessageExchanges = false;
ep.UseMessageRetry(r => r.Interval(kvp.Value.RetryCount, kvp.Value.RetryInterval));
ep.Bind("ingest-amazon-products", x => BindForEndpoint(x, kvp.Value.RoutingKey));
BindExchange(ep, kvp.Value.RoutingKey, kvp.Value.Assembly, kvp.Value.Type);
ep.ConfigureConsumers(serviceProvider);
});
}
});
Now, the code above works, but not in the way I intended, as messages without matching routing keys still get delivered to my consumers. I mean - if the kvp.Value.RoutingKey configuration value is X, and the producer produces a message with a routing key of Y, the consumer listening for X's will get a Y message. How to fix that?
ReceiveEndpointmethod and setting the type inside, but your client is also a receiver of messages and mine is not. Should I do it anyway? - Marek M.ReceiveEndpointand it works. Could you please post an answer, I could accept? :) - Marek M.