Sharing gRPC .proto Files with Nuget Packages Made Easy

Sanket Naik's
4 min readAug 26, 2023

--

Introduction:

Hello tech enthusiasts, welcome back to another post. This time diving into the world of gRPC!

Recently, I encountered a specific use case that left me searching the depths of the internet for solutions and content, only to find a scarcity of relevant material. Fueled by the desire to help others facing a similar challenge, I embarked on a journey to gather information from various sources and consolidate it into a comprehensive blog.

In this article, I aim to provide you with a well-structured solution, saving you from the struggles I faced when dealing with this particular use case. I sincerely hope you find this knowledge useful and that it helps you out when you run into similar problems. Let’s get going!

Use Case:

I have multiple gRPC services, each with its own .proto file. Some of these .proto files share the same contract across multiple services.
Now, I want to make these .proto files easily accessible to everyone(client/consumers).

To achieve this, we’re packaging these .proto files into Nuget packages and publishing them on a private Nuget feed.

However, there’s a challenge. Accessing these .proto files isn’t straightforward. There are specific conventions you need to follow to access them. It might seem tricky at first, but once you understand the process, it becomes quite simple.

How to make it work?

Lets break it into three steps:
1. Packing into .csproj(project) and publishing to .nupkg(Nuget package).
2. Consuming the .proto file at client side.
3. Testing the gRPC code.

1. Packing into .csproj and publishing to .nupkg:

1) In your Grpc.Shared project (one that you will push to Nuget and consumed by gRPC clients), create/add your .proto files (I am putting under ‘Proto’ directory below).

2) Mark the .proto files as a <Content/> inside <ItemGroup>:

3) Release the nuget package. Below command will generate .nupkg to the directory .\nupkgs. You can then push to the Nuget feed.
Ref Example.

dotnet pack --configuration Release --output .\nupkgs

2. Consuming the .proto file at client side.

  1. Install the Grpc.Shared .nupkg/Nuget in your gRPC client project.
  2. Add GeneratePathProperty="true" property to package reference.
  3. Add prefix $(PkgGrpc_Shared)\content\ while including the Protobuf reference. ('\content' is the directory name where all the content files are available by default.)
    Note that, $(PkgGrpc_Shared) is by conventions where $(PkgGrpc_Shared)will be resolved to Grpc.Shared Nuget directory (the variable name starts with Pkg and is followed by the package name when . is replaced by _)

3. Testing the gRPC code

  1. If you are new to gRPC client, you can refer this page by Microsoft.
  2. Install package (Grpc.Shared) in your gRPC client project.
  3. Test and try consuming autogenerated code for .proto

FAQ errors and Debugs:

If you haven’t correctly configured the Protobuf file path, you will likely encounter the following errors:

for incorrect project variable ‘$(PkgGrpc_Shared)’
‘$(PkgGrpc_Shared)’ is correct, but .proto file name/path is incorrect.

Bonus Tip:

You can explore/view the .proto files in Visual Studio at your client project like below.

Summary

  1. This blog explores sharing gRPC .proto files via Nuget packages.
  2. It details a three-step process:
    1. Packing .proto files into .csproj and publishing to .nupkg.
    2. Consuming
    .proto files in the client project.
    3. Testing the gRPC code.
  3. Specific instructions are provided with image example for each step.
  4. The blog also addresses common errors related to Protobuf file configuration.
  5. A bonus tip on exploring .proto files in Visual Studio is included.

--

--

Sanket Naik's

Senior Software Developer | FullStack NetCore | Clean Code | Angular | Blazor | Tech enthusiast