I'm trying to use protobuf-net with C# positional record types, and I've encountered this exception:
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)
This is what GetServerProcessInfoResponse looks like:
[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
);
The code works fine if I change GetServerProcessInfoResponse into a regular C# class with gettable and settable properties. I was hoping however that records could work too, because they avoid a whole lot of nullability warnings. System.Text.Json has support for deserializing records for example, which has to work against the same limitations.
I couldn't find anything in the docs, issues or here on StackOverflow, so maybe I'm bad at searching, or maybe the answer isn't out there yet. :-)
The protobuf-net repository also doesn't seem to have any unit tests that try to serialize / deserialize a c# record, it only contains "RecordTypeTests" which seems to be checking if records can be cloned?