Skip to content

Instantly share code, notes, and snippets.

@fzankl
Created January 24, 2021 09:12
Show Gist options
  • Save fzankl/7d2d82ad2df4eb277d00450b6564fd8f to your computer and use it in GitHub Desktop.
Save fzankl/7d2d82ad2df4eb277d00450b6564fd8f to your computer and use it in GitHub Desktop.
Send and receive a protobuf in C#
using System;
using System.IO;
using Google.Protobuf;
namespace GrpcSamples
{
public class Program
{
public static void Main(string[] args)
{
var foo = new Foo
{
Id = 1,
Description = "FooMessage"
};
// Write to any stream
var stream = new MemoryStream();
foo.WriteTo(stream);
stream.Seek(0, SeekOrigin.Begin);
// Create Foo object from any stream
var parsedFoo = Foo.Parser.ParseFrom(stream);
Console.WriteLine($"Foo: Id {parsedFoo.Id} - {parsedFoo.Description}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment