17

What needs to be done in order to serialize enums with protobuf-net? I'm getting the below error when serializing a class having an enum property, if the DataMember attribute is removed from the property declaration it works fine.

"System.InvalidOperationException: Only data-contract classes (and lists/arrays of such) can be processed"

Kimi
  • 13,621
  • 9
  • 55
  • 84
  • Do you have an example? This should work fine... – Marc Gravell Nov 10 '10 at 18:28
  • Hello Marc! Thanks for post about deseriazing List using Protobuf.net. I've got the same issue. I will post example below. – Andrew Kalashnikov Nov 15 '10 at 05:32
  • Just a heads up, this question is being discussed on [meta](https://meta.stackoverflow.com/questions/367085). Is there a reason you rolled back the edit including an MCVE? Was your situation different from Andrews? – Rob May 02 '18 at 00:56

2 Answers2

18

I suspect they are actually 2 different scenarios, but with regard to the code sample added by Andrew, this is because it can't figure out (in advance) what it is going to do with regards to default values (by default, data is treated as optional at the receiver). There are 3 ways of fixing this:

1: add an enum with value 0 (since 0 is always the CLI default value for zeros), for example

public enum SiteType
{
    Error = 0,
    ...

2: tell it which value to use by default:

[ProtoMember(10), DefaultValue(SiteType.Partition)]
public SiteType Type { get; set; }

3: tell the engine that it really doesn't need to worry about it, i.e. that it is going to have a value:

[ProtoMember(10, IsRequired = true)]
public SiteType Type { get; set; }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Thanks. Now it works! With protobuf-net I reduce my traffic,speed to 10 times with my WCF service. It's realy power nice way for rising perfomance. I think it should be common way work with this protocol\net. Unfortunality there aren't a lot of samples about applying protobuf.net with WCF – Andrew Kalashnikov Nov 16 '10 at 12:48
  • 1
    @Andrew - feedback noted. I'll try to improve that. – Marc Gravell Nov 16 '10 at 14:18
  • My problem was that we have been serializing a list of objects and one of the properties did not have IsRequired = true. I guess it would be nice in such case to include an inner exception. – Kimi Nov 16 '10 at 17:21
  • @Al - ok; I'll tweak v2 to make that more obvious. In a way though, I'm glad it was the same issue and that it is now resolved. – Marc Gravell Nov 16 '10 at 17:24
  • Does this answer still apply to v2.0.0.480? – Dan Ling May 09 '12 at 18:16
  • @Otter largely, but v2 also introduces the ability to just treat enums as vanilla integers rather than applying the enum handling, making it much easier to work with some scenarios; IIRC this is enabled automatically for `[Flags]` enums. Do you have a specific scenario in mind that I can assist with? – Marc Gravell May 09 '12 at 18:43
4

Sample:

[DataContract]
[ProtoContract]
public enum SiteType
{
    [EnumMember]
    [ProtoEnum]
    Site = 1,
    [EnumMember]
    [ProtoEnum]
    Partition = 2,
    [EnumMember]
    [ProtoEnum]
    Module = 3
}

[DataContract]
[Serializable]
[ProtoContract]
public class SiteDTO
{
    [DataMember]
    [ProtoMember(1)]
    public int Id { get; set; }
    ...
    [DataMember]
    [ProtoMember(10)]
    public SiteType Type { get; set; }
}
Andrew Kalashnikov
  • 3,073
  • 5
  • 34
  • 57
  • Thanks for the example Andrew. I have that locally now and will work through it. I *imagine* it is simply getting over enthusiastic about deciding it is a complex object. I guess in my WCF usage I simply never decorated enums with `[DataContract]`. I haven't checked yet, but I assume it works OK if you remove the attributes on the enum? – Marc Gravell Nov 15 '10 at 11:48
  • when I try that, the error I get is about the defualt enum value (is that what you see?) which isn't really the same as reported by Al. However, I've added a reply that (I think) addresses *your* question. – Marc Gravell Nov 15 '10 at 12:36