src/Serializers/Orleans.Serialization.Protobuf/README.md
Microsoft Orleans Serialization for Protobuf provides Protocol Buffers (Protobuf) serialization support for Microsoft Orleans using Google.Protobuf. This package integrates Google's official Google.Protobuf library with Orleans, allowing you to use Protocol Buffers messages in your grain interfaces and implementations.
To use this package, install it via NuGet:
dotnet add package Microsoft.Orleans.Serialization.Protobuf
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Orleans.Hosting;
using Orleans.Serialization;
var builder = Host.CreateApplicationBuilder(args)
.UseOrleans(siloBuilder =>
{
siloBuilder
.UseLocalhostClustering()
// Configure Protobuf as a serializer
.AddSerializer(serializerBuilder => serializerBuilder.AddProtobufSerializer());
});
// Run the host
await builder.RunAsync();
This package supports types generated from .proto files using Google.Protobuf. For detailed information on creating Protobuf messages and configuring your project, see Create Protobuf messages for .NET apps.
Once you have defined your Protobuf messages and configured code generation, you can use them directly in your grain interfaces:
using Orleans;
using MyApp.Models;
public interface IMyGrain : IGrainWithStringKey
{
Task<MyProtobufClass> GetData();
Task SetData(MyProtobufClass data);
}
public class MyGrain : Grain, IMyGrain
{
private MyProtobufClass _data;
public Task<MyProtobufClass> GetData() => Task.FromResult(_data);
public Task SetData(MyProtobufClass data)
{
_data = data;
return Task.CompletedTask;
}
}
Note: Google.Protobuf collection types (RepeatedField<T>, MapField<TKey, TValue>, and ByteString) are automatically supported.
For more comprehensive documentation, please refer to: