tags:

views:

70

answers:

1

I am using protobuf-net r282 and when I call Serialize I get the InvalidOperationException error message "Only data-contract classes (and lists/arrays of such) can be processed (error processing Object)". At the point where protobuf-net Serializer.Serialize is being called the object in question has been cast to an interface. Is there a way to get around this?

Here's the code:

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ProtoBuf;
using System;

namespace EventStore.Core
{
public interface Message
{
}

public interface Event : Message
{
    Guid Id { get; set; }

    int Version { get; set; }
}

[DataContract]
public class InventoryItemCreated : Event
{
    [DataMember(Order = 1)]
    public Guid Id { get; set; }

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

    [DataMember(Order = 3)]
    public string Name { get; set; }

    public InventoryItemCreated(Guid id, string name)
    {
        Id = id;
        Name = name;
    }

    public InventoryItemCreated()
    {

    }
}

public class DefaultSerializer
{
    private readonly IFormatter formatter = new BinaryFormatter();

    public byte[] Serialize<T>(T graph) where T : class
    {
        if (default(T) == graph)
            return null;

        using (var stream = new MemoryStream())
        {
            this.Serialize(graph, stream);
            return stream.ToArray();
        }
    }

    public virtual void Serialize<T>(T graph, Stream output) where T : class
    {
        this.formatter.Serialize(output, graph);
    }

    public T Deserialize<T>(byte[] serialized) where T : class
    {
        if (null == serialized || 0 == serialized.Length)
            return default(T);

        using (var stream = new MemoryStream(serialized))
            return this.Deserialize<T>(stream);
    }

    public virtual T Deserialize<T>(Stream input) where T : class
    {
        return (T)this.formatter.Deserialize(input);
    }
}

public class ProtoBufSerializer : DefaultSerializer
{
    public override void Serialize<T>(T graph, Stream output)
    {
        Serializer.Serialize<T>(output, graph);
    }

    public override T Deserialize<T>(Stream input)
    {
        return Serializer.Deserialize<T>(input);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ProtoBufSerializer serializer = new ProtoBufSerializer();
        InventoryItemCreated item = new InventoryItemCreated(Guid.NewGuid(), "Widget");
        byte[] buffer = serializer.Serialize((Message)item);
    }
}

}

A: 

This is something that I have been looking at for "v2", but it is incomplete. I have been sent a patch for v1 that does this and which I can share, but I haven't rigorously tested this. Let me know if you want this patch file.

Marc Gravell
I'd be willing to give it a shot and see if it works for us.
Mark J Miller
I made the patch, its been working well, see http://stackoverflow.com/questions/3523225/protobuf-net-and-interface-support for my post on this. There was only a few small changes required but havent had Marc's keen eye look over it thoroughly and pick a hole in it yet
wal
@wal - I had to rebuild my v1 VM (nothing to do with the patch). Now done, so hope to look at this now.
Marc Gravell
@Mark - drop me an email (see my profile page) and I'll send it you.
Marc Gravell
Got the patch. Thank you. I had to finish up some other stuff and haven't been able to get back to it. I should be able to look at this today and let you know if it fixes this for us.
Mark J Miller
After some discussion we decided not to go with a patch, this post over on DDDCQRS group gave us what we needed. http://groups.google.com/group/dddcqrs/browse_thread/thread/e1f5e0cac45c6add
Mark J Miller