【发布时间】:2020-06-12 06:59:03
【问题描述】:
我需要序列化我在运行时使用 ProBuf.Serializer 创建的数据表,ProBuf.Serializer 在 protobuf-net Nuget 下可用。下面附上我使用过的代码示例。我收到错误 ProtoBuf.Serialzer.Serialize(流,表);有人可以帮我解决这个问题吗?
public static void Main()
{
try
{
Process process = new Process();
process.StartInfo.FileName = @"E:\POC\Test\GrpcServer\bin\Debug\netcoreapp3.0\GrpcServer.exe";
process.Start();
List<ChannelOption> channelOptions = new List<ChannelOption>()
{
new ChannelOption(ChannelOptions.MaxSendMessageLength, int.MaxValue)
};
Channel channel = new Channel("localhost:5005", ChannelCredentials.Insecure, channelOptions);
var client = new TestingService.TestingServiceClient(channel);
DataTable table = CreateTable(100000);
Console.WriteLine("Starting Serialization");
DateTime serializationStartTime = DateTime.Now;
MemoryStream stream = new MemoryStream();
ProtoBuf.Serializer.Serialize(stream, table);
stream.Seek(0, SeekOrigin.Begin);
DateTime serializationEndTime = DateTime.Now;
byte[] arr = stream.ToArray();
ByteString data = ByteString.CopyFrom(arr);
Console.WriteLine("Completed Serialization");
Console.WriteLine("Started Communication with Grpc Server");
DateTime startGrpcTime = DateTime.Now;
client.RecieveData(new PBData() { Data = data });
DateTime endGrpcTime = DateTime.Now;
Console.WriteLine("Grpc communication ended");
Console.WriteLine($"Serialization time :{(serializationEndTime - serializationStartTime).TotalSeconds}");
Console.WriteLine($"Grpc Communication time :{(endGrpcTime - startGrpcTime).TotalSeconds}");
}
catch (Exception ex)
{
Console.WriteLine($"Error:{ex.Message}");
}
}`
【问题讨论】:
-
在发布我的答案后,我偶然发现了这个:github.com/dotarj/protobuf-net-data - 注意:这不是我的代码 - 我从未使用过、评估过、审查过它,或者任何东西 - 所以它不是官方推荐:但是:粗略一看,它看起来可能会做你想做的事!
标签: c# grpc protobuf-net serialization