Skip to content

Instantly share code, notes, and snippets.

@fzankl
Created January 24, 2021 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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