Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Protobuf 3.0.0 assemblies for C#

In our project we successfully use Google Protobuf for C++. Now there is a need to compile the same *.proto file to use it in C# code. I downloaded the recent Protobuf version 3.0.0-alpha-3. It provides support of proto2 format for C#, which is sufficient for me. I can build my *.proto file successfully and get a *.cs file. However, when I add the resulting *.cs file to my C# project and try to build, I receive compiler errors like these: "The type or namespace name 'Google' could not be found in the global namespace (are you missing an assembly reference?)" This is the place, where the error happens:

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: DiagramExport.proto
#pragma warning disable 1591, 0612, 3021
#region Designer generated code

using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;

Now I do not find any DLL etc. in the release ZIP available on the project page, which I could include as reference in my C# project. Only protoc.exe and some *.proto file are there. My simple question is: Where do I get those assemblies?

(Remark: I tried to build the project protobuf-csharp-3.0.0-alpha-3 from sources following the instructions in the README file, but failed to build it with Visual Studio 2013 Update 4 "out of the box"; I get a number of compiler errors.)

like image 902
Vivit Avatar asked Aug 17 '15 11:08

Vivit


People also ask

What is protobuf C?

This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c , a pure C library that implements protobuf encoding and decoding, and protoc-c , a code generator that converts Protocol Buffer . proto files to C descriptor code.

What languages does protobuf support?

Protocol buffers currently support generated code in Java, Python, Objective-C, and C++.

Are proto2 and proto3 compatible?

Using proto2 Message TypesIt's possible to import proto2 message types and use them in your proto3 messages, and vice versa. However, proto2 enums cannot be used directly in proto3 syntax (it's okay if an imported proto2 message uses them).

What version of protobuf does gRPC use?

While not mandatory, gRPC applications often leverage Protocol Buffers for service definitions and data serialization. Most of the example code from this site uses version 3 of the protocol buffer language (proto3). The protocol buffer compiler, protoc , is used to compile .


2 Answers

After reading this and this documentation page I discovered that there is a possibility to install the Protocol Buffers NuGet package for my project by executing the following command in the Package Manager Console:

Install-Package Google.ProtocolBuffers

The console is accessible in Visual Studio 2013 via TOOLS --> NuGet Package Manager --> Package Manager Console. The manager downloaded the package and I got two references "Google.ProtocolBuffers" and "Google.ProtocolBuffers.Serialization" in my project which made the compiler happy. It works perfect now!

like image 128
Vivit Avatar answered Nov 15 '22 20:11

Vivit


Have a look at the release notes here

Under the C# (Beta) section you will find:

Breaking: Preconditions is renamed to ProtoPreconditions
Breaking: GeneratedCodeInfo is renamed to GeneratedClrTypeInfo

So it seems the protoc.exe that come with the Grpc.Tools package generate "old" code. I replaced that protoc.exe with this one and recompiled (regenerated) my classes which fixed the problem.

like image 20
pieterlouw Avatar answered Nov 15 '22 21:11

pieterlouw