Search code examples
.netasp.net-coreprotocol-buffersgrpccurrency

How do you import the google.type.Money type in an aspnetcore project using Grpc?


I have an aspnetcore application that's using Grpc, and is currently working fine with a simple message containing only strings.

I need to add a new message that will contain money values, and I saw that there is a money.proto type available by importing google/protobuf. However, when I try to import it, I get an error.

I added the following in my proto file:

import "google/protobuf/money.proto";

I expected the Grpc Money type to be generated during the build, and complete successfully.

However, the build fails and I get the following errors:

Import "google/protobuf/money.proto" was not found or had errors.
"google.protobuf.Money" is not defined

I found this question asked previously, and the answer was to disable protobuf support in ReSharper. However, when I checked this setting, the box was already unchecked by default, as shown below:

resharper-protobuf-setting-disabled

This follow-up comment stated that these proto files are available in the Grpc.Tools nuget package, which I have installed in my project: enter image description here

This similar question had the same answer.

I was able to find the file itself on github here, and used it to confirm that I had the casing correct (import statement lower-case m, type usage upper-case M). I noticed that the package for this file was 'google/type' and not 'google/protobuf', so I tried changing my import statement and usages to match, but it had no effect on the problem, same errors.

I checked here to see if there was any documentation on how to get this working, and all they had to say was this: enter image description here

Which, if I'm reading it correctly, suggests that if I have a working Grpc setup, then I should have this available.

Finally, I found this article suggesting that these files must be manually copy/pasted into your project, but that seems quite primitive? Surely there's a way to pull these in automatically through nuget or something?

I've been unable to find any further guidance on the topic. Any help would be greatly appreciated.


Solution

  • My apologies it's taken me this long to get back with an update, but I believe I've got it all sorted now.

    The tl;dr is that I needed to do the following to fix the problem:

    1. Install the Grpc.AspNetCore nuget package in both the server and the client.
    2. Add the googleapis repo as a git submodule.
    3. Add a <Protobuf Include="../path/to/money.proto" /> item element in both the client and server project files.
    4. Add the AdditionalImportDirs attribute to the <Protobuf /> item element for "my" proto file so it can import the money proto file.

    Server csproj:

    <ItemGroup>
        <Protobuf Include="Proto\example.proto" GrpcServices="Server" AdditionalImportDirs="..\..\BuildingBlocks\googleapis" />
        <Protobuf Include="..\..\BuildingBlocks\googleapis\google\type\money.proto" />
    </ItemGroup>
    

    Client csproj:

    <ItemGroup>
        <Protobuf Include="../GrpcExample.Server/Proto/example.proto" GrpcServices="Client" AdditionalImportDirs="..\..\BuildingBlocks\googleapis" />
        <Protobuf Include="..\..\BuildingBlocks\googleapis\google\type\money.proto" />
    </ItemGroup>
    

    I've created a github repo with an example client/server project that shows working code on how to set this all up, and a README that walks you through step-by-step how to go from 2 web API projects to Money working over gRPC.

    example-grpc-aspnetcore