Skip to content

RPC Protocol

Antoine Théate edited this page Jun 5, 2025 · 3 revisions

Protocol Explanation

Request/reply, often called RPC, is a popular pattern to implement with a messaging broker like RabbitMQ. One of the popular use cases is a microservices based architecture where one service requests data from another service.

A complete documentation is available here.
Note that RabbitMQ version 4.0 is the minimal version that supports the RPC protocol.

Code Example

On following example, IRpcServerService and IRpcCleintService will be used. No extension method for the IServiceCollection is available so it will be required to register them at startup.

Micro Service A (Acts like RPC Server)

var rpcServerService = serviceProvider.GetRequiredService<IRpcServerService>();
var disposable = await this.rpcServerService.ListenForRequestAsync<int, string>("Primary", "rpc_request", OnReceiveAsync);

....

private static Task<string> OnReceiveAsync(int arg)
{
   return Task.FromResult(arg.ToString(CultureInfo.InvariantCulture));
}

Micro Service B (Acts like RPC Client)

var rpcServerService = serviceProvider.GetRequiredService<IRpcClientService<string>>();
var observable = await rpcClientService.SendRequestAsync("Primary", "rpc_request", 41);
observable.Subscribe(response => Console.WriteLine(response));

Results:
When the Micro Service B pushes the message, the RPC server will apply a "ToString()" operation on the received int value and response to the client with the string representation. A new line will be written in the Console of Micro Service B with the content as "41".

Clone this wiki locally