I'm moving my gRPC app from .net framework to .net core
I have two main proto
files as follows
/Proto/common/common.proto
/Proto/my-service.proto
my-service.proto
importing common.proto
as simple as this:
common.proto
syntax="proto3";
package common;
option csharp_namespace = "Koko.Wawa";
message ValidationFailure {
string property_name = 1;
string error_message = 2;
}
message ValidationResult {
bool is_valid = 1;
repeated ValidationFailure errors = 2;
}
my-service.proto
syntax="proto3";
package google.protobuf;
option csharp_namespace = "Koko.Wawa";
import "common/common.proto"; // << in gRPC .net Core Template it gives error
message CreateDraftPackageRequest {
string package_type = 1;
repeated int32 client_ids = 2;
}
message CreateDraftPackageResponse {
int32 id = 1;
string package_type = 2;
int64 created_on = 3;
int32 package_delivery_status = 4;
common.ValidationResult validation_result = 5;
}
service ExportPackageService {
rpc CreateDraftPackage (CreateDraftPackageRequest) returns (CreateDraftPackageResponse);
}
In a Powershell command, I used protoc
command line to generate my C# classes. Also, I modified it to make it work with .net core also.
$exe = "${env:USERPROFILE}\.nuget\packages\grpc.tools\2.24.0-pre1\tools\windows_x64\protoc.exe"
$plugin = "${env:USERPROFILE}\.nuget\packages\grpc.tools\2.24.0-pre1\tools\windows_x64\grpc_csharp_plugin.exe"
& $exe -I .\Protos\Common --csharp_out .\Output\Common .\Protos\Common\common.proto --grpc_out .\Output\Common --plugin=protoc-gen-grpc=$plugin --csharp_opt=file_extension=.g.cs --grpc_opt=internal_access
& $exe -I .\Protos --csharp_out .\Output .\Protos\export-package-service.proto --grpc_out .\Output --plugin=protoc-gen-grpc=$plugin --csharp_opt=file_extension=.g.cs --grpc_opt=internal_access
Everything works perfectly with this approach. But I tried to depend on the new gRPC template in .net core 3, you will keep getting a File not found an error! In
import "common/common.proto"; // <<<<<
Change import
to the path relative to the project root:
import "Proto/Common/common.proto";
If you still want to use exe
directly, change -I
argument to the project dir, eg .\
:
& $exe -I .\ --csharp_out .\Output .\Protos\export-package-service.proto --grpc_out .\Output --plugin=protoc-gen-grpc=$plugin --csharp_opt=file_extension=.g.cs --grpc_opt=internal_access
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With