使用Protoc生成c#代码

2020-04-20  本文已影响0人  95加不满

1. 下载工具

#地址
https://github.com/protocolbuffers/protobuf/releases

下载的文件: protoc-3.11.4-win64.zip

2.设置环境变量

C:\Users\Administrator>protoc --version
libprotoc 3.11.4

3.生成.cs文件

目录结构如下:
protos
-- peer
---- chaincode.proto
---- proposal.proto
---- proposal_response.proto
--token
---- expectations.proto
---- transaction.proto

3.1 简单模式

进入token目录下,执行如下命令

protoc --csharp_out=./   ./transaction.proto

文件transaction.proto

syntax = "proto3";

option go_package = "github.com/hyperledger/fabric/protos/token";
option java_package = "org.hyperledger.fabric.protos.token";

message TokenTransaction {
    // action carries the content of this transaction.
    oneof action {
        PlainTokenAction plain_action = 1;
    }
}
...省略其他代码

3.2 复杂模式 (文件中含有import命令)

进入peer目录下,执行如下命令

# 指定目录:--proto_path=D:\protos   
# 绝对路径:D:\protos\peer\proposal.proto 、--csharp_out=D:\protos\peer
protoc D:\protos\peer\proposal.proto --csharp_out=D:\protos\peer --proto_path=D:\protos

文件proposal.proto

 syntax = "proto3";

option go_package = "github.com/hyperledger/fabric/protos/peer";
option java_package = "org.hyperledger.fabric.protos.peer";
option java_outer_classname = "ProposalPackage";

package protos;

import "peer/chaincode.proto";
import "peer/proposal_response.proto";
import "token/expectations.proto";

message SignedProposal {

    // The bytes of Proposal
    bytes proposal_bytes = 1;

  // Signaure over proposalBytes; this signature is to be verified against
  // the creator identity contained in the header of the Proposal message
  // marshaled as proposalBytes
    bytes signature = 2;
}
...省略其他代码

复杂模式的方案来源:

If your proto file is, say, foo/bar/baz/qux.proto, then your --proto_path can be any of ., foo, foo/bar, or foo/bar/baz, but it cannot be corge nor can it be /absolute/path/to/foo/bar/baz, etc. because those are not prefixes of the file name. The idea is that the proto path should specify your "top level source directory", i.e. the directory that all your imports are relative to. So if you import this file as import "baz/qux.proto" then your proto path should be foo/bar. – Kenton Varda

[参考文章]https://stackoverflow.com/questions/21134066/error-using-import-in-proto-file

上一篇 下一篇

猜你喜欢

热点阅读