我有一个有线问题,我正在使用protobuf-net为C#生成一个基于Google Protocol Buffer消息的文件,然后将其上传到我公司的一个服务器.
我在C#中创建了一个生成.cs文件的工具,然后我使用它的类(来自.cs文件)来填充消息中的所有必填字段,之后我调用了Serializer.Serialze()函数它为我创建了所需的文件.
但是,这就是问题,我有另一个文件(同一个文件)在另一个用C++编写的工具中创建(使用我使用的相同.proto文件),但是当我试图将我的文件上传到我们的服务器时我收到一个错误的错误.
我将这2个文件与"Win Merge"软件进行了比较,我注意到与C++工具中生成的文件相比,3个不同行(每个文件中7000行以外)的差异很小.
下面是从Win Merge工具捕获的2行示例(在C++左侧,在右侧C#上):


我注意到差异在于矩形(我不明白它有什么意义)和它们内部的字节...
这是我正在使用的.proto文件:
message Package {
message ArchDepend {
message Arch {
required string version = 1;
required string description = 2;
}
message Firmware {
required string version = 1;
required string description = 2;
required bytes file = 3;
repeated string from_version = 4;
}
message Rsu {
required string version = 1;
required string description = 2;
required bytes file = 3;
}
required Arch arch = 1;
optional …Run Code Online (Sandbox Code Playgroud) 我们使用协议缓冲区在本机C++应用程序之间进行通信,但也通过protobuf-net r666在本机C++应用程序和.NET应用程序(都是VS2012)之间进行通信.我们在可选元素的has_函数上严重依赖C++.
例如,如果我们有一个带有字段可选bool的消息,则可以是它未设置,设置为true或设置为false.
在C++中,可以使用函数has_field检查,如果设置,则可以使用get_field函数获取内容.如果未设置,并且调用了get_field,则get返回默认值,如果未显式设置,则返回false(对于布尔值).
这在C++中完美地工作,但是,在protobuf-net中,我们似乎找不到has_函数的等价物,并且,当收到消息时,该字段被添加到消息中并且其内容被设置为默认值,是假的.具有默认值的字段不是灾难,但问题是没有has_函数来检查它是否在消息中设置.
请告知这是否是一个错误,或者我们是否错过了protobuf-net中的内容并且这实际上是可行的
Thx提前.维姆
我是protobuf-net的初学者,所以这可能只是一些愚蠢的初学者错误.但我无法找到这个问题:
我有一个类被序列化到磁盘定义如下:
[ProtoContract]
public class SerializableFactors
{
[ProtoMember(1)]
public double?[] CaF {get;set;}
[ProtoMember(2)]
public byte?[] CoF { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和测试定义如下:
if (File.Exists("factors.bin"))
{
using (FileStream file = File.OpenRead("factors.bin"))
{
_factors = Serializer.Deserialize<SerializableFactors>(file);
}
}
else
{
_factors = new SerializableFactors();
_factors.CaF = new double?[24];
_factors.CaF[8] = 7.5;
_factors.CaF[12] = 1;
_factors.CaF[18] = 1.5;
_factors.CoF = new byte?[24];
_factors.CoF[8] = 15;
_factors.CoF[12] = 45;
_factors.CoF[18] = 25;
using (FileStream file = File.Create("factors.bin"))
{
Serializer.Serialize(file, _factors);
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上如果文件不存在,我创建一个具有默认值的对象并将其序列化为磁盘.如果文件存在,我会将其加载到内存中.
但是我加载文件的结果不是我在保存到磁盘之前创建的.我创建了长度为24的数组,它们在插槽8,12和18中具有值.但是反序列化对象具有长度为3的数组,其中包含我的值. …
我正在构建一个带有套接字通信的C#服务器和python客户端应用程序.服务器将序列化的对象列表发送到客户端,但我不知道(也找不到)如何在python中反序列化列表.任何帮助,将不胜感激.
我有以下课程
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class Foo
{
public int foo { get; set; }
[ProtoIgnore]
public Bar bar { get; set; }
public int ToMD5Hash()
{
var md5 = MD5CryptoServiceProvider.Create();
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize<Foo>(ms, this);
var hash = md5.ComputeHash(ms.ToArray());
return BitConverter.ToInt32(hash, 0);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在调用ToMD5Hash时遇到异常.它表示No serializer defined for type: SomeNamespace.Bar即使属性使用ProtoIgnore属性进行修饰.
注意:如果我删除ImplicitFields并使用ProtoMember,序列化将起作用.
难道我做错了什么?
protobuf-net 是否支持命名元组的序列化?
例如
[ProtoMember(1)]
protected readonly SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)> FuturesCurveData;
Run Code Online (Sandbox Code Playgroud) 我从Google.Protobuf Git中了解到有一些关于.NET Core支持的评论但是当我从Git获得源代码的副本时,Google.Protobuf项目在.依赖关系中列出.NetFramework 4.5以及.NetStandard 1.0 .
在csharp文件夹中的Google.Protobuf Git Depot上,还有一些关于.NET Core支持的评论.
此外,通过通过问题跟踪器上连接到仓库去,我碰到这个线程从大约一年回有关获取Google.Protobuf发挥好与.NET的核心.
谁能给我一个关于Google.Protobuf是否支持.NET Core的明确答案?
protocol-buffers protobuf-net protobuf-csharp-port .net-core uwp
我正在尝试使用内存映射文件将 C# 对象从一个进程发送到另一个进程,并且我正在尝试使用 BinaryFormatter 或 protobuf-net。两者都不起作用 - 显然是因为我必须使用固定长度的字节数组,而 protobuf-net 需要一个长度完全正确的数组?
使用 protobuf-net,我在反序列化时得到这个异常:“ProtoException: 'Unconsumed data left in the buffer; this表明输入损坏'在这一行:“message1 = Serializer.Deserialize(memoryStream);
这是我的代码。在这一点上,我只是尝试一个简单的例子,以便让它在基本级别上工作:这是我想在程序之间发送的对象:
[ProtoContract]
public class IpcMessage
{
public IpcMessage() { }
[ProtoMember(1)]
public string title { get; set; }
[ProtoMember( 2 )]
public string content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是发送 IpcMessage 对象的(简化 - 我删除了同步)代码:
static void SampleSend()
{
// Create the memory-mapped file which allows 'Reading' and 'Writing'
using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen( "MyMmfName", 1024, MemoryMappedFileAccess.ReadWrite ))
{
// …Run Code Online (Sandbox Code Playgroud) 在 .proto 文件中使用导入时出现“找不到文件”错误。我正在使用 Rider,但在使用 Visual Studio 时遇到了同样的问题。
第一个原型文件:
syntax = "proto3";
import "/fileToImport.proto";
service GreeterAPI {
rpc SayHello (SayHelloRequest) returns (SayHelloResponse);
}
message SayHelloRequest {
string name = 1;
}
message SayHelloResponse {
string answer = 1;
}
Run Code Online (Sandbox Code Playgroud)
我要导入的第二个 proto 文件:
syntax = "proto3";
message Foo {
string bar = 1;
}
Run Code Online (Sandbox Code Playgroud)
这两个文件在项目目录中彼此相邻。
.csprjo 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.10.1" />
<PackageReference Include="Grpc.Core" Version="2.25.0" />
<PackageReference Include="Grpc.Tools" Version="2.25.0" />
<Protobuf Include="**/*.proto" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
如果我在没有导入行的情况下构建项目,一切都很好。但是使用导入行我得到“找不到文件”
我知道我可以使用 …
我正在尝试将 protobuf-net 与 C# 位置记录类型一起使用,但我遇到了这个异常:
10:18:48.048 [EROR] #010 (Microsoft.AspNetCore.Server.Kestrel) Connection id ""0HM4NDHMUB3C6"", Request id ""0HM4NDHMUB3C6:00000003"": An unhandled exception was thrown by the application.
Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. ProtoException: No parameterless constructor found for Bidirectional.Demo.Common.Contracts.Server.GetServerProcessI
nfo.GetServerProcessInfoResponse", DebugException="ProtoBuf.ProtoException: No parameterless constructor found for Bidirectional.Demo.Common.Contracts.Server.GetServerProcessInfo.GetServerProcessInfoResp
onse
at ProtoBuf.Internal.ThrowHelper.ThrowProtoException(String message, Exception inner) in /_/src/protobuf-net.Core/Internal/ThrowHelper.cs:line 70
at ProtoBuf.Meta.TypeModel.ThrowCannotCreateInstance(Type type, Exception inner) in /_/src/protobuf-net.Core/Meta/TypeModel.cs:line 1666
at proto_12(State& , GetServerProcessInfoResponse )
at ProtoBuf.Internal.Serializers.SimpleCompiledSerializer`1.ProtoBuf.Serializers.ISerializer<T>.Read(State& state, T value)
at ProtoBuf.ProtoReader.State.ReadAsRoot[T](T value, ISerializer`1 serializer)
at ProtoBuf.ProtoReader.State.DeserializeRoot[T](T value, ISerializer`1 …Run Code Online (Sandbox Code Playgroud)