Search code examples
importgrpcprotocol-buffers

Protobuf import failure


Does anyone know where I can find an example of a gRPC protobuf file that imports from a different file and uses a protobuf message in a return? I can't find any at all.

I have a file...

syntax = "proto3";
package a1;
import "a.proto";
service mainservice {
  rpc DoSomething(...) returns (a.SomeResponse) {}


}

a.proto is also in the same directory and also compiles by itself. The error messages I'm getting are: "a.SomeResponse" is not defined. mainfile.proto: warning: Import a.proto but not used.


Solution

  • Found the answer... need to make sure the package name of a.proto is used when specifying the object imported (eg: a_package_name.SomeResponse). Example:

    base.proto

    syntax = "proto3";
    option csharp_namespace = "Api.Protos";
    package base;
    message BaseResponse {
        bool IsSuccess = 1;
        string Message = 2;
    }
    

    user.proto

    syntax = "proto3";
    import "Protos/base.proto";
    option csharp_namespace = "Api.Protos";
    package user;
    
    message UserCreateResponse {
        base.BaseResponse response = 1;
    }