Skip to content

Example

This page demonstrates the complete RPC workflow using the ASP.NET Core integration package.

The examples are conceptually based on the demo application:

ServiceBus.Ms.Demo.Net10

Architecture

graph LR

    Client

    RabbitMQ

    ASPNET["ASP.NET Core"]

    Controller

    Client --> RabbitMQ

    RabbitMQ --> ASPNET

    ASPNET --> Controller

Request Contract

public sealed record CalculatePriceRequest(
    Guid ProductId,
    int Quantity);

Response Contract

public sealed record CalculatePriceResponse(
    decimal TotalPrice);

ASP.NET Core Endpoint

[ApiController]
[Route("api/pricing")]
public sealed class PricingController : ControllerBase
{
    [HttpPost("calculate")]
    public CalculatePriceResponse Calculate(
        CalculatePriceRequest request)
    {
        return new CalculatePriceResponse(
            request.Quantity * 10m);
    }
}

Client Request

var response =
    await publisher.PublishAsync<
        CalculatePriceRequest,
        CalculatePriceResponse>(
            request,
            cancellationToken);

Runtime Flow

sequenceDiagram

    participant Client

    participant Publisher

    participant RabbitMQ

    participant ASPNET

    Client->>Publisher: Request

    Publisher->>RabbitMQ: Publish

    RabbitMQ->>ASPNET: Deliver

    ASPNET-->>RabbitMQ: Response

    RabbitMQ-->>Publisher: Deliver Response

    Publisher-->>Client: Result

When To Use RPC

Recommended scenarios:

  • validation services;
  • calculation services;
  • lookup services;
  • synchronous business operations.

When Not To Use RPC

Avoid RPC when:

  • eventual consistency is acceptable;
  • responses are not required;
  • workflows are naturally event-driven.

In those scenarios, traditional Publish/Subscribe messaging is generally a better choice.