Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly migrate from WCF DatacontractSerializer to Protobuf-net?

We have a huge application with a lot of classes. We are currently porting this .net application to IPad with Monotouch. We have some problems with the DataContractSerializer and we would like to use Marc Gravell's protobuf-net serializer.

The communication between the client and the server is managed by a WCF Service.

A WCF service is made of one Interface exposed to the client and the server, and one implementation of this interface on the server.

The interface looks like that:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    SomeObject MyFunction(SomeObject myObject);
}

The server side implementation looks like that:

[ServiceBehavior(...)]
public class MyService
{
    public SomeObject MyFunction(SomeObject myObject)
    {
    }
}

Our classes looks like that:

[DataContract]
public class MyClass
{
    [DataMember]
    public int SomeProp {get; set;}

    [OnSerialized]
    public void OnSerialized(StreamingContext context)
    {
    }
}

So here are my questions:

  • What would be the changes to do to my classes, wcf interface and wcf implementation.

  • How would I replace the default WCF DataContractSerializer to the Protobuf Serializer.

Please note that on monotouch, I only have access to Protobuf and Protobuf.Meta namespaces.

[EDIT] I found a way to swap the serializer runtime: Custom WCF DataContractSerializer

The above solution uses the DataContractSerializerOperationBehavior. Does Protobuf-net provides such behavior?

like image 336
Jean-Philippe Leclerc Avatar asked Nov 04 '22 16:11

Jean-Philippe Leclerc


1 Answers

In all honesty, I am unfamiliar with the WCF options available to you in monmotouch; they are very different between regular .NET and Silvelight, for example - and I see no reason to assume that monotouch has the ability to swap serializer at runtime (which "full" .NET does, at least under the MS version). This makes it hard to do the transition silently, as we can't wrestle control from DataContractSerializer.

As such, IMO the simplest option is to seize control of the data manually, and send raw byte[] - ideally with MTOM encoding enabled if monotouch can do that. Then once you have your byte[] the world is your mollusc, as they say.

Re changes to your types... well, MyFunction() is an oddity, in that it doesn't transfer any data, so I'm not sure what you want me to suggest on that one. With MyClass, all it needs is a unique number (unique within the type, not globally) per member, i.e.

[DataContract]
public class MyClass
{
    [DataMember(Order=1)] // <==== this provides the 1 as the key
    public int SomeProp {get; set;}

    // see below re callback
}

You also have a serialization callback; these are fully supported, but it expects to find a familiar pattern - StreamContext is not one that I know of (although it should work with StreamingContext and a few others).

Finally, note that by default protobuf-net executes the constructor, which is different to DataContractSerializer. If you desire, you can suppress this via:

[DataContract(SkipConstructor=true)]
public class MyClass {...}

If I've missed the intent here, let me know.

Note there are also ways of doing all the configuration without changing/adding any attributes if you prefer.

like image 111
Marc Gravell Avatar answered Nov 09 '22 12:11

Marc Gravell