无法导入外部原型文件 - 在命令行中工作但在 .net core 3 RC1 中不工作
Cannot import external proto file - Works in commandline but not in .net core 3 RC1
我正在将我的 gRPC 应用程序从 .net 框架迁移到 .net 核心
我有两个主要的 proto
文件如下
/Proto/common/common.proto
/Proto/my-service.proto
my-service.proto
导入 common.proto
就这么简单:
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;
}
我的-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);
}
在 Powershell 命令中,我使用 protoc
命令行生成我的 C# 类。另外,我对其进行了修改,使其也可以与 .net core 一起使用。
$exe = "${env:USERPROFILE}\.nuget\packages\grpc.tools.24.0-pre1\tools\windows_x64\protoc.exe"
$plugin = "${env:USERPROFILE}\.nuget\packages\grpc.tools.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
一切都适用于这种方法。但是我尝试依赖 .net core 3 中的新 gRPC 模板,你会不断收到一个文件未找到的错误!在
import "common/common.proto"; // <<<<<
将import
改为相对于项目根目录的路径:
import "Proto/Common/common.proto";
如果您仍想直接使用exe
,请将-I
参数更改为项目目录,例如.\
:
& $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
我正在将我的 gRPC 应用程序从 .net 框架迁移到 .net 核心
我有两个主要的 proto
文件如下
/Proto/common/common.proto
/Proto/my-service.proto
my-service.proto
导入 common.proto
就这么简单:
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;
}
我的-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);
}
在 Powershell 命令中,我使用 protoc
命令行生成我的 C# 类。另外,我对其进行了修改,使其也可以与 .net core 一起使用。
$exe = "${env:USERPROFILE}\.nuget\packages\grpc.tools.24.0-pre1\tools\windows_x64\protoc.exe"
$plugin = "${env:USERPROFILE}\.nuget\packages\grpc.tools.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
一切都适用于这种方法。但是我尝试依赖 .net core 3 中的新 gRPC 模板,你会不断收到一个文件未找到的错误!在
import "common/common.proto"; // <<<<<
将import
改为相对于项目根目录的路径:
import "Proto/Common/common.proto";
如果您仍想直接使用exe
,请将-I
参数更改为项目目录,例如.\
:
& $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