Back to Orleans

Microsoft Orleans Serialization for Protobuf

src/Serializers/Orleans.Serialization.Protobuf/README.md

10.1.03.0 KB
Original Source

Microsoft Orleans Serialization for Protobuf

Introduction

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.

Getting Started

To use this package, install it via NuGet:

shell
dotnet add package Microsoft.Orleans.Serialization.Protobuf

Example - Configuring Protobuf Serialization

csharp
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();

Using Protobuf Types with Orleans

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:

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

Documentation

For more comprehensive documentation, please refer to:

Feedback & Contributing