Search code examples
c#grpc

add gRPC JSON Transcoding to exisitng C# gRPC Server. Having problem with importing google api's


I'm trying to add JSON Transcoding to a gRPC Server simple test using greet.proto that was working perfectly (except for the fact that I can't connect to it from the outside on Windows 2016 Server due to the HTTP/2 issue. Server 2016 does Not seem to fully support it for gRPC, but Transcoding give me HTTP/1).

I'm using a GrpcShared shared project class library to store my Proto file between a Console client, Maui client and WPF client.

It was working perfectly before I added the changes for JSON Transcoding.

The GrpcShared class library project just had the example greet.proto file in the first level. No Protos folder.

I added a google folder and an api folder inside and placed annotations.proto and http.proto (the latest versions I believe) inside the google/api folders.

When I try to compile the GrpcShared class project, I get an error of:

Error CS0234 The type or namespace name 'Api' does not exist in the namespace 'Google' (are you missing an assembly reference?) GrpcShared G:\Projects\GrpcService\GrpcShared\obj\Debug\net7.0\Greet.cs 33 Active

which is of course the generated file.

The GrpcShared project definition is as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
 
    <ItemGroup>
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
    <PackageReference Include="Google.Protobuf" Version="3.22.1" />
    <PackageReference Include="Grpc.Net.Client" Version="2.52.0" />
    <PackageReference Include="Grpc.Tools" Version="2.53.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
 
 
  <ItemGroup>
    <Compile Remove="HelloReply.cs" />
  </ItemGroup>

  <ItemGroup>
    <SupportedPlatform Include="browser" />
    <Protobuf Include="greet.proto" />
  </ItemGroup>

</Project>


I found an example project for JSON Transcoding.

https://github.com/grpc/grpc-dotnet/tree/master/examples/Transcoder

I followed their basic setup and also the slightly more recent video from .NET that demonstrates the feature:

https://youtu.be/et_2NBk4N4Y

One line in the example project definition perplexes me?

<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />

as there Is no 'Proto' folder in the project? Only the 'Protos' folder and the above line refers to .. Both?

I've tried moving the annotations.proto and http.proto to the main level and changing the import of annotations.proto but .. that just made things worse?

So I'm a but stumped on something that is obviously Trivial and fixed with a few lines of code .. but Not obvious to Me!!


Solution

  • I got the transcoding example working by adding some missing using statements.

    The problem with the 'api' error was in the way I setup the Protos structure.

    The virtual Protos folder (possibly called 'Solution Folders') gets Created by this line in the project:

        <Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
    

    When setup 'correctly' as in the example, the api and google physical folders will never show up in the solution explorer .. even with 'show all files' on.

    So follow that transcoding example github project Exactly using the Exact same file structure .. to setup json transcoding.