'onserialized handler called twice

I play with protobuf-net.grpc. I have shared class library with this class:

[DataContract, ProtoInclude(50, typeof(BetterResult))]
public class MultiplyResult
{

    public MultiplyResult()
    {
        this.Counter = 1;
    }

    [DataMember(Order = 1)]
    public int Result { get; set; }

    [DataMember(Order = 2)]
    public int Counter { get; set; }

    [DataMember(Order = 3)]
    public States SelectedState { get; set; }

    [DataMember(Order = 4)]
    public List<MultiplyRequest> Requests
    {
        get; set;
    }

    [ProtoAfterDeserialization]
    protected virtual void OnDeserialized(StreamingContext context)
    {
        Console.WriteLine("result deserialized " + Environment.GetEnvironmentVariable("EnvName"));
    }

    [ProtoAfterSerialization]
    protected virtual void OnSerialized(StreamingContext context)
    {
        
        Console.WriteLine("result serialized " +this.Counter  + Environment.GetEnvironmentVariable("EnvName")  );
        this.Counter++;
    }

}

I can see that ProtoAfterSerialization handler is called twice.

By the way, don't you know, why setting Counter = 0 in constructor throws this exception?

$exception {"Status(StatusCode="Internal", Detail="Error starting gRPC call. InvalidDataException: Unexpected data after finished reading message.", DebugException="System.IO.InvalidDataException: Unexpected data after finished reading message.\r\n at Grpc.Net.Client.StreamExtensions.ReadMessageAsync[TResponse](Stream responseStream, GrpcCall call, Func2 deserializer, String grpcEncoding, Boolean singleMessage, CancellationToken cancellationToken)\r\n at Grpc.Net.Client.Internal.GrpcCall2.RunCall(HttpRequestMessage request, Nullable`1 timeout)")"} Grpc.Core.RpcException

Thank you

Tom



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source