RPC method

How RPC methods are mapped to C#.

A Protobuf RPC method named RpcName in service Greeter is mapped to abstract method RpcNameAsync in interface IGreeter and to abstract method RpcNameAsync in interface IGreeterService.

The mapped method name is RpcName converted to Pascal case, followed by Async.

For example:

protobuf
syntax = "proto3";
package visitor_center;
// A simple Greeter service.
service Greeter {
// Creates a personalized greeting.
rpc Greet (GreetRequest) returns (GreetResponse);
}
C#
namespace VisitorCenter;
public partial interface IGreeter
{
Task<GreetResponse> GreetAsync(
GreetRequest message,
IFeatureCollection? features = null,
CancellationToken cancellationToken =
default);
}
public partial interface IGreeterService
{
ValueTask<GreetResponse> GreetAsync(
GreetRequest message,
IFeatureCollection features,
CancellationToken cancellationToken);
}

While the two methods are similar, please note they are not the same:

  • the client-side method returns a Task<T> while the service method returns a ValueTask<T>
  • the features parameter is nullable and defaults to null only in the client-side method
  • the cancellation token parameter has a default value only in the client-side method

A Protobuf RPC method can accept a stream of input messages, return a stream of output messages, or both, using the stream keyword. The mapped C# methods are the same as for a unary RPC method, except a stream input is mapped to a stream parameter (instead of a message parameter), and a stream output is mapped to a stream return value.

The C# type of a stream depends on its direction:

  • an outgoing stream is a stream your code gives to the generated code, namely the stream you pass to a client-side method or the stream your service method returns
  • an incoming stream is a stream the generated code gives to your code, namely the stream you receive from a client-side method or the stream your service method receives
Stream directionC# type
Outgoing
Incoming

T is the C# class that protoc generates for the streamed message. For example:

protobuf
syntax = "proto3";
package example;
service Sensor {
// Client streaming
rpc Upload (stream Reading) returns (UploadSummary);
// Server streaming
rpc Monitor (MonitorRequest) returns (stream Reading);
// Bidirectional streaming
rpc Calibrate (stream Reading) returns (stream Reading);
}
C#
namespace Example;
public partial interface ISensor
{
Task<UploadSummary> UploadAsync(
IAsyncEnumerable<Reading> stream,
IFeatureCollection? features = null,
CancellationToken cancellationToken = default);
Task<IAsyncStream<Reading>> MonitorAsync(
MonitorRequest message,
IFeatureCollection? features = null,
CancellationToken cancellationToken = default);
Task<IAsyncStream<Reading>> CalibrateAsync(
IAsyncEnumerable<Reading> stream,
IFeatureCollection? features = null,
CancellationToken cancellationToken = default);
}
public partial interface ISensorService
{
ValueTask<UploadSummary> UploadAsync(
IAsyncStream<Reading> stream,
IFeatureCollection features,
CancellationToken cancellationToken);
ValueTask<IAsyncEnumerable<Reading>> MonitorAsync(
MonitorRequest message,
IFeatureCollection features,
CancellationToken cancellationToken);
ValueTask<IAsyncEnumerable<Reading>> CalibrateAsync(
IAsyncStream<Reading> stream,
IFeatureCollection features,
CancellationToken cancellationToken);
}

When you give an async enumerable to the generated code, the IceRPC + Protobuf integration either iterates over all its messages or cancels the iteration early, typically because the peer doesn't want more messages. This early cancellation is communicated to your async enumerable using the EnumeratorCancellationAttribute, as demonstrated by the server side of the Stream example application.

IAsyncStream<T> is an IAsyncEnumerable<T> that is also IDisposable: it owns the underlying transport stream from which the messages are decoded, and you must dispose it when you're done with it. The simplest approach is a using declaration:

C#
using IAsyncStream<Reading> readings = await sensor.MonitorAsync(new MonitorRequest());
await foreach (Reading reading in readings)
{
Console.WriteLine(reading.Value);
}

An IAsyncStream<T> can be enumerated at most once.

If you set the deprecated option to true on an RPC method, the mapped client-side methods (such as IGreeter.GreetAsync and GreeterClient.GreetAsync) are marked Obsolete. The mapped service method (such as IGreeterService.GreetAsync) is not marked Obsolete, so you can keep implementing a deprecated RPC method without warnings.

If you set the idempotency_level option to NO_SIDE_EFFECTS or IDEMPOTENT, the client-side generated implementation sets the field RequestFieldKey.Idempotent in the outgoing request it creates; otherwise, it leaves this field unset.

Was this page helpful?

CookiesYour privacy
This website uses cookies to analyze traffic and improve your experience.
By clicking "Accept," you consent to the use of these cookies. You can learn more about our cookies policy in our Privacy Policy.