Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import protobuf well-known types in AspNet Core gRPC Service

Tags:

I', scratching my head about how to import "google/protobuf/empty.proto" to my proto file. What I'm doing so far:

  1. In my .csproj file, I added the following:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
       <PropertyGroup>
          <TargetFramework>netcoreapp3.0</TargetFramework>
       </PropertyGroup>
    
       <ItemGroup>
          <Protobuf Include="C:\Users\me\.nuget\packages\google.protobuf.tools\3.10.0\tools\google\protobuf\*.proto" GrpcServices="Server"/>
          <Protobuf Include="Protos\*.proto" GrpcServices="Server" />
       </ItemGroup>
    
       <ItemGroup>
          <PackageReference Include="Grpc.AspNetCore" Version="2.23.2" />
          <PackageReference Include="System.Reactive" Version="4.1.6" />
       </ItemGroup>
    
    </Project>
    
  2. In my project folder, I have a folder called "Protos" where I have a test.proto file in which I would like to use the "empty.proto" like:

import "google/protobuf/empty.proto";

However, I get the error File not found in project. I also do not like to specify the "3.10.0" in the ItemGroup. What is the correct way to use google protobuf well-known types?

like image 599
kovac Avatar asked Oct 06 '19 16:10

kovac


People also ask

Does gRPC use Protobuf?

Protobuf is the most commonly used IDL (Interface Definition Language) for gRPC. It's where you basically store your data and function contracts in the form of a proto file.

Can proto3 import proto2?

It's possible to import proto3 message types and use them in your proto2 messages, and vice versa. However, proto2 enums cannot be used in proto3 syntax.

What is the Protobuf file format?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

How do you use any in Protobuf?

To use the Any type, you must import the google/protobuf/any. proto definition. In the C# code, the Any class provides methods for setting the field, extracting the message, and checking the type. Protobuf's internal reflection code uses the Descriptor static field on each generated type to resolve Any field types.


1 Answers

if you are using resharper just follow the pic

go to r# option

uncheck the selected protobuf

reason of this issue : Though Protobuf is purely path-based and will work once the file in found on your hard disk ReSharper needs all the files added to solution in order to process them and Current implementation of included files' search inside protobuf support does not allow to find the files outside the solution.

have good time

like image 63
Amin Fayazi Avatar answered Sep 30 '22 17:09

Amin Fayazi