16

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

Vivit
  • 1,919
  • 2
  • 24
  • 40
  • I don't know what is that library you are trying to use, but I would start with [protobuf.net](https://code.google.com/p/protobuf-net/). – Sinatr Aug 17 '15 at 12:08
  • @Sinatr As I understand, I try to use the "official" distribution by Google. At least, the documentation on Google's site points to this project (see https://developers.google.com/protocol-buffers/ ) I do not want to use protobuf.net, because we already use protoc.exe to compile our *.proto file for C++ and Java. – Vivit Aug 17 '15 at 12:13
  • Google has nothing to do with .Net. So someone has to do a *dirty job* of porting either wrapper to c++ library or a source code into c#. I don't see `c#` on linked page. Simply use protobuf.net from your C# code. – Sinatr Aug 17 '15 at 12:15
  • 1
    @Sinatr: You are incorrect. Google has adopted Jon Skeet's C# implementation of Protobuf into the official Protobuf Github repository. https://github.com/google/protobuf/tree/master/csharp Marc's implementation has historically been more popular but Jon's implementation is more similar to Google's Java implementation. (And Jon is a Google employee, etc.) – Kenton Varda Aug 18 '15 at 03:03
  • @KentonVarda, I'll repeat, Google has nothing to do with .Net (which is made and supported by MS). They could (in theory, some day) add c# support, but they don't have to, this is my point. I know there are many implementations of protobuf, but we are talking about one you want to use in production, right? Have you read the page you linked? P.s.: Ironically I was using google to find protobuf.net, but I fail to find quickly Jon's one, so I just edit comment and leave there protobuf.net link only. – Sinatr Aug 18 '15 at 06:56
  • @Sinatr I really don't know what you're trying to argue here. Obviously Google doesn't own .net, but they do own protobuf-csharp-port (that's Jon Skeet's implementation) and have merged it into the official Protobuf source repository. You seem to be arguing that Google doesn't officially support nor plan to support protobuf in C#, and I'm saying that's not correct. Proto3 is still in alpha, so sure, you might not want to use it in production yet, but I think the OP recognizes that. – Kenton Varda Aug 18 '15 at 19:08
  • @KentonVarda, your comments are very useful, thank you. – Sinatr Aug 19 '15 at 07:04
  • I just wan't to clarify all of the above for myself and any one else.. In my C# application ,if i generate my messages from my .proto file using protoc compiler and use the nuget package Google.ProtocolBuffers. (which is jon skeets implementation of the Protocol buffers protocol ) aka 'protobuf-csharp-port' I'm good to go. – eran otzap Oct 25 '16 at 22:43

2 Answers2

13

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!

Vivit
  • 1,919
  • 2
  • 24
  • 40
  • 5
    FYI, a little update: Google Protocol Buffers C# 3.0.0-beta2 is also available `Install-Package Google.Protobuf -Pre` – GraehamF Mar 15 '16 at 19:45
5

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.

pacholik
  • 7,596
  • 8
  • 43
  • 50
pieterlouw
  • 61
  • 2
  • 3