protobuf-net是否支持C#9位置记录类型?
c#
我正在尝试将 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 serializer)
at ProtoBuf.Meta.TypeModel.Deserialize[T](ReadOnlySequence`1 source, T value, Object userState)
at ProtoBuf.Grpc.Configuration.ProtoBufMarshallerFactory.ContextualDeserialize[T](DeserializationContext context)
这是 GetServerProcessInfoResponse 的样子:
[ProtoContract]
public record GetServerProcessInfoResponse(
[property: ProtoMember(1)] TimeSpan TotalProcessorTime,
[property: ProtoMember(2)] TimeSpan UserProcessorTime,
[property: ProtoMember(3)] TimeSpan PrivilegedProcessorTime,
[property: ProtoMember(4)] string CurrentMemoryUsage,
[property: ProtoMember(5)] string PeakMemoryUsage,
[property: ProtoMember(6)] int ActiveThreads
);
如果我将 GetServerProcessInfoResponse 更改为具有 gettable 和 settable 属性的常规 C# 类,则代码工作正常。然而,我希望记录也可以工作,因为它们避免了大量的可空性警告。例如,System.Text.Json 支持反序列化记录,这必须克服相同的限制。
我在文档、问题或 StackOverflow 上找不到任何内容,所以也许我不擅长搜索,或者答案还没有出来。:-)
protobuf-net 存储库似乎也没有任何尝试序列化/反序列化 ac# 记录的单元测试,它只包含“RecordTypeTests”,它似乎在检查是否可以克隆记录?
回答
是的,有两种不同的方式。
使用您现有的代码,最简单的选择是告诉 protobuf-net 执行 voodoo 以克服没有可用构造函数的事实;幸运的是,这很简单:
[ProtoContract(SkipConstructor = true)]
public record GetServerProcessInfoResponse(
[property: ProtoMember(1)] TimeSpan TotalProcessorTime,
[property: ProtoMember(2)] TimeSpan UserProcessorTime,
[property: ProtoMember(3)] TimeSpan PrivilegedProcessorTime,
[property: ProtoMember(4)] string CurrentMemoryUsage,
[property: ProtoMember(5)] string PeakMemoryUsage,
[property: ProtoMember(6)] int ActiveThreads
);
然而!随着 v3 代码的最新版本,它还将推断出您在纯粹位置记录的情况下要专门做什么,即以下内容也应该起作用,并且或多或少意味着相同的事情(作为一个细节:它将在这种情况下实际使用构造函数,而不是通过伏都教创建一个实例然后踩踏值):
public record GetServerProcessInfoResponse(
TimeSpan TotalProcessorTime,
TimeSpan UserProcessorTime,
TimeSpan PrivilegedProcessorTime,
string CurrentMemoryUsage,
string PeakMemoryUsage,
int ActiveThreads
);