Question

Currently we are using a WCF Service with NetTcpBinding. We do have dataobjects which have an enum as property. We already have figured out, that out of the box these values are serialized as string representation, check here: How do I serialize an enum value as an int?

We do want to avoid that because this is just communication overhead!

As we are going to switch serialization to protobuf-net on the wire quite soon, so I am asking myself if defining an additional integer property is worth the time spent. We have several enums, e.g.

[Flags]
public enum Permission
{
    None = 0,
    Read = 1,
    Write = 2
}

How is the binary representation of flags enum in protobuf-net on the wire?

How does it deal with non-defined enum values (e.g. Read | Write)?

I also found some related answer by Marc Gravell here, but it might be outdated (2009): Error while using ProtoBuf-Net with flags enum

Était-ce utile?

La solution

Enums that are marked [Flags] are sent as a straight integer, to avoid issues with combinations of flags not being formally defined (the protobuf specification describes enums, but does not define bitwise combination). Read | Write is integer 3, which is varint-encoded into a single byte, and sent as-is. The details of varint-encoding are detailed here: https://developers.google.com/protocol-buffers/docs/encoding#varints

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top