IceRPC Protobuf integration package
$ dotnet add package IceRpc.ProtobufThe IceRPC framework allows you to make RPCs with the serialization format and IDL of your choice. It provides full support for both Slice and Protobuf.
The IceRpc.Protobuf NuGet package is part of the IceRPC + Protobuf integration, and includes two .NET assemblies:
protoc-gen-icerpc-csharpPackage | Source code | Documentation | Examples | API reference
// Protobuf contract
syntax = "proto3";
package visitor_center;
option csharp_namespace = "VisitorCenter";
// Represents a simple greeter.
service Greeter {
rpc Greet (GreetRequest) returns (GreetResponse);
}
message GreetRequest {
string name = 1;
}
message GreetResponse {
string greeting = 1;
}
// Client application
using IceRpc;
using VisitorCenter;
await using var connection = new ClientConnection(new Uri("icerpc://localhost"));
// GreeterClient is a struct generated by protoc-gen-icerpc-csharp.
var greeter = new GreeterClient(connection);
var request = new GreetRequest { Name = Environment.UserName };
GreetResponse response = await greeter.GreetAsync(request);
Console.WriteLine(response.Greeting);
await connection.ShutdownAsync();
// Server application
using IceRpc;
using VisitorCenter;
// Create a server that dispatches all requests to the same service, an instance of
// Chatbot.
await using var server = new Server(new Chatbot());
server.Listen();
// Wait until the console receives a Ctrl+C.
await CancelKeyPressed;
await server.ShutdownAsync();
// IGreeterService is an interface generated by protoc-gen-icerpc-csharp.
// The [ProtobufService] attribute instructs the Protobuf Service source generator
// (provided by IceRpc.Protobuf.Generators.dll) to implement IDispatcher by directing
// "Greet" requests to the GreetAsync method.
[ProtobufService]
internal partial class Chatbot : IGreeterService
{
public ValueTask<GreetResponse> GreetAsync(
GreetRequest message,
IFeatureCollection features,
CancellationToken cancellationToken)
{
Console.WriteLine($"Dispatching Greet request {{ name = '{message.Name}' }}");
return new(new GreetResponse { Greeting = $"Hello, {message.Name}!" });
}
}