---
title: "RPC method"
description: "How RPC methods are mapped to C#."
url: https://docs.icerpc.dev/protobuf/language-mapping/rpc-method
docs_index: https://docs.icerpc.dev/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.icerpc.dev/llms.txt).

# RPC method

How RPC methods are mapped to C#.

## C# mapping

A Protobuf RPC method named *RpcName* in service `Greeter` is mapped to abstract method *RpcName*Async in interface `IGreeter` and to abstract method *RpcName*Async 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);
}
```

```csharp
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

### Streaming RPC methods

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 direction | C# type |
| --- | --- |
| Outgoing | [`IAsyncEnumerable<T>`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1) |
| Incoming | [`IAsyncStream<T>`](https://code.icerpc.dev/csharp/0.7.x/api/reference/IceRpc.IAsyncStream-1.html) |

`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);
}
```

```csharp
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);
}
```

#### Sending a stream

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`](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.enumeratorcancellationattribute), as demonstrated by the server side of the [Stream example](https://github.com/icerpc/icerpc-csharp/tree/0.7.x/examples/protobuf/Stream) application.

#### Receiving a stream

[`IAsyncStream<T>`](https://code.icerpc.dev/csharp/0.7.x/api/reference/IceRpc.IAsyncStream-1.html) 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:

```csharp
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.

### deprecated option

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](https://learn.microsoft.com/en-us/dotnet/api/system.obsoleteattribute). The mapped service method (such as `IGreeterService.GreetAsync`) is not marked [Obsolete](https://learn.microsoft.com/en-us/dotnet/api/system.obsoleteattribute), so you can keep implementing a deprecated RPC method without warnings.

### idempotency\_level option

If you set the `idempotency_level` option to `NO_SIDE_EFFECTS` or `IDEMPOTENT`, the client-side generated implementation sets the field [RequestFieldKey.Idempotent](https://code.icerpc.dev/csharp/0.7.x/api/reference/IceRpc.RequestFieldKey.html#IceRpc_RequestFieldKey_Idempotent) in the outgoing request it creates; otherwise, it leaves this field unset.
