IceRpc.Slice for C#.
$ dotnet add package IceRpc.SliceThe 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.Slice NuGet package is part of the IceRPC + Slice integration, and includes two .NET assemblies:
Package | Source code | Documentation | Examples | API reference
// Slice contract
module VisitorCenter
/// Represents a simple greeter.
interface Greeter {
/// Creates a personalized greeting.
/// @param name: The name of the person to greet.
/// @returns: The greeting.
greet(name: string) -> string
}
// Client application
using IceRpc;
using VisitorCenter;
await using var connection = new ClientConnection(new Uri("icerpc://localhost"));
// GreeterProxy is a struct generated by the Slice compiler; its implementation uses
// IceRpc.Slice.
var greeter = new GreeterProxy(connection);
string greeting = await greeter.GreetAsync(Environment.UserName);
Console.WriteLine(greeting);
await connection.ShutdownAsync();
// Server application
using IceRpc;
using IceRpc.Features;
using IceRpc.Slice;
using VisitorCenter;
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 the Slice compiler.
// The [SliceService] attribute instructs the Slice Service source generator (provided by
// IceRpc.Slice.Generators.dll) to implement IDispatcher by directing "greet" requests to
// the GreetAsync method.
[SliceService]
internal partial class Chatbot : IGreeterService
{
public ValueTask<string> GreetAsync(
string name,
IFeatureCollection features,
CancellationToken cancellationToken)
{
Console.WriteLine($"Dispatching greet request {{ name = '{name}' }}");
return new($"Hello, {name}!");
}
}