Home > Net >  Get the received message from the sender to a specific controller : MassTransit in ASP.NET Core Web
Get the received message from the sender to a specific controller : MassTransit in ASP.NET Core Web

Time:03-08

I am quite new in Masstransit and rabbitMq and I want to establish a communication between two Web APIs.

The goal: if the service A receive a request to make something but the it needs some additional information from another service B, I want to make this connection between the two services using Masstransit and RabbitMq, but I want to get the returned result from the service B in the action method in the specific controller in service A (the API that requested the information).

I already created the integration of Masstransit and RabbitMq, but I am getting the message in the consumer and I want get that result in the action method that is waiting for the response from the service B.

If there are any examples or any hint to adapt the solution or anything may help that would very appreciated

Thanks

CodePudding user response:

Use the request client to generate a request, and get the response. An example in an API controller is shown in the scoped filter sample, along with many other of the MassTransit samples.

[ApiController]
[Route("[controller]")]
public class CheckInventoryController :
    ControllerBase
{
    readonly IRequestClient<CheckInventory> _client;

    public CheckInventoryController(IRequestClient<CheckInventory> client)
    {
        _client = client;
    }

    [HttpGet]
    public async Task<IActionResult> Get(string sku)
    {
        Response<InventoryStatus> response = await _client.GetResponse<InventoryStatus>(new {sku});

        return Ok(response.Message);
    }
}
  • Related