4

I'm using dotnet standard 2.0 (Visual Studio 2017) for gRPC. This is how my whole project looks like:

My whole project with 2 proto files

Messages.proto

syntax = "proto3";
package Messages;

message IdRequest{
    int32 id = 1;
}

message NameResponse{
    string name=1;
}

messages.proto file properties

Name.proto

syntax = "proto3";
package Services;

import public "proto/messages.proto";

service NameService{
    rpc GetNameById(Messages.IdRequest) returns (Messages.NameResponse);
}

name.proto file properties

Common.proj

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="proto\messages.proto" />
    <None Remove="proto\name.proto" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.10.1" />
    <PackageReference Include="Grpc" Version="2.24.0" />
    <PackageReference Include="Grpc.Core" Version="2.24.0" />
    <PackageReference Include="Grpc.Tools" Version="2.24.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <Protobuf Include="proto\messages.proto" />
    <Protobuf Include="proto\name.proto" />
  </ItemGroup>

</Project>

The project builds successfully however the final Common.dll has no Messages namespace and I cannot really reference IdRequest or NameResponse.

Object Browser VS2017

So where am I making the mistake that hides Messages namespace?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
  • If you look at the methods of NameServiceBase/NameServiceClient, the return type and parameter should be of your expected types, which should tell you where they are? – Marc Gravell Nov 02 '19 at 09:37
  • I'm not able to reproduce this. I see the Messages namespace. please provide a screenshot of your properties on those proto file. [pasteboard.io - screenshot](https://pasteboard.co/IEO4oOw.png) – Brett Caswell Nov 02 '19 at 10:08
  • when I add a reference to common, the code doesn't compile because it can't find the signature for any of the messages. so even though the service is declared and the common is built successfully, it cannot be used by any project. – Simple Fellow Nov 02 '19 at 10:09
  • hmm.. your question states it builds, but your comment suggests it isn't compiling.. please add this information to your question. Also, are you running VS2017 as administrator – Brett Caswell Nov 02 '19 at 10:12
  • @BrettCaswell I just updated the question with the file properties. – Simple Fellow Nov 02 '19 at 10:13
  • @BrettCaswell why do i need to run it as administrator? it's just that common.dll cannot be used. – Simple Fellow Nov 02 '19 at 10:15
  • I not suggesting it's is required to run it as administrator (though the idea of using a compiler that is probably in your NuGet PackageReference AppData folder does raise the consideration to me). At any rate, I was asking whether you are running as administrator, which it appears you aren't with your response. go ahead and test building while running in administrator. – Brett Caswell Nov 02 '19 at 10:23
  • doesn't work even when running as administrator. However one very odd thing. after restarting VS, the Messages namespace appeared. so I made the change and recompiled and poof!...it's gone again even running as administrator. – Simple Fellow Nov 02 '19 at 10:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201756/discussion-between-brett-caswell-and-simple-fellow). – Brett Caswell Nov 02 '19 at 11:06

2 Answers2

3

In your project file into the Protobuf-tag you need to add the GrpcServices attribute or else no code is created:

  <ItemGroup>
    <Protobuf Include="proto\messages.proto" GrpcServices="Server" />
    <Protobuf Include="proto\name.proto" GrpcServices="Server" />
  </ItemGroup>
Michael G.
  • 1,139
  • 8
  • 6
1

I know this is so old, but in case it is helpful for someone else. The documentation for gRPC has greatly improved since the asking of this question.

Looking at the tags associated, but also from the discussions and screenshots from here, I'm going to make the assumption that the project was working with csharp, as this makes a difference. I believe the namespaces are not generated correctly because the option "csharp_namespace" is missing.

The service will also have its namespace changed because of this amendment.

Message.proto:

syntax = "proto3";
package Messages;
option csharp_namespace = "Messages";

message IdRequest{
    int32 id = 1;
}

message NameResponse{
    string name=1;
}

Name.proto:

syntax = "proto3";
package Services;
option csharp_namespace = "Messages";

import "proto/Messages.proto";

service NameService{
    rpc GetNameById(Messages.IdRequest) returns (Messages.NameResponse);
}
kibblewhite
  • 169
  • 2
  • 6
  • yes, it was asked in 2019 but there might be many searching for the same so the answer is always valuable and welcome. thanks for sharing the knowledge. – Simple Fellow Dec 01 '21 at 14:15