I have two separate services one called Teacher.API and Process.API. Teacher service generates POST requests and there Teacher details will send into the Process Service, according to my need.
This is how I configure MassTrasit in both services.
in the Teacher.API
services.AddMassTransit(x =>
{
x.UsingRabbitMq((context, config) =>
{
config.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
});
and in the Process.API.
services.AddMassTransit(x =>
{
x.AddConsumer<TeacherConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ReceiveEndpoint("teacherQueue", ep =>
{
ep.PrefetchCount = 16;
ep.UseMessageRetry(r => r.Interval(2, 100));
ep.ConfigureConsumer<TeacherConsumer>(context);
});
});
});
And in the Teacher Controller, my POST request as follows,
[HttpPost]
public async Task<IActionResult> Registraion(Teacher Teacher)
{
if (Teacher != null)
{
Teacher.BookedOn = DateTime.Now;
Uri uri = new Uri("rabbitmq://localhost/teacherQueue");
var endPoint = await _bus.GetSendEndpoint(uri);
await endPoint.Send(Teacher);
return Ok();
}
return BadRequest();
}
here I'm creating a new teacherQueue
queue and push the Teacher object.
My problem is, RabbitMQ management plugin, does not show teacherQueue
in the queue section but showing it in the Exchanges.
My consumer part is as follows, this consumer is also not hit.
public class TeacherConsumer : IConsumer<Teacher>
{
public Task Consume(ConsumeContext<Teacher> context)
{
var data = context.Message;
return Task.CompletedTask;
}
}
Any reason? what did I do wrong?
CodePudding user response:
You're missing the AspNetCore package, and the call to:
services.AddMassTransitHostedService();
Which actually starts the bus.