bun*_*eeb 8 c# proto .net-core grpc asp.net-core
我正在将我的 gRPC 应用程序从 .net 框架转移到 .net 核心
我有两个主要proto文件如下
/Proto/common/common.proto
/Proto/my-service.proto
Run Code Online (Sandbox Code Playgroud)
my-service.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;
}
Run Code Online (Sandbox Code Playgroud)
我的服务.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);
}
Run Code Online (Sandbox Code Playgroud)
在 Powershell 命令中,我使用protoc命令行来生成我的 C# 类。另外,我修改了它以使其也适用于 .net core。
$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
Run Code Online (Sandbox Code Playgroud)
一切都用这种方法完美地工作。但是我尝试依赖 .net core 3 中的新 gRPC 模板,您将不断收到 File not found 错误!在
import "common/common.proto"; // <<<<<
Run Code Online (Sandbox Code Playgroud)
fen*_*xil 12
更改import为相对于项目根目录的路径:
import "Proto/Common/common.proto";
Run Code Online (Sandbox Code Playgroud)
如果你还想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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3314 次 |
| 最近记录: |