Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protobuf-net v2 and Monotouch : How does it mix?

I have been trying to use protobuf-net with MonoTouch but I have no idea how, and despite having heard that it is possible, I haven't been able to find any tutorial or any example that actually work.

It was confirmed by Marc Gravell on his blog that it does work on MonoTouch. I have also looked through the blogs of the two people he states in this article, but I haven't found anything related to protobuf.

Having no lead on the subject, i decided to download protobuf-net and try it out anyway. So I created the following object for testing purposes :

[ProtoContract]
public class ProtoObject
{
    public ProtoObject()
    {

    }

    [ProtoMember(1)]
    public byte[] Bytes { get; set; }
}

and I tried to send it through WCF from a service running on windows using a [ServiceContract] interface with

[OperationContract]
ProtoObject GetObject();

but the instance of ProtoObject recieved on the device is always null. This is not really unexpected since i have read that to make protobuf-net work with WCF you need to modify the app.config/web.config.

It's a little hard to accomplish since a MonoTouch project has no app.config, but I did not yet give up. To replace the app.config, I tried to add the ProtoEndpointBehavior to the client's endpoint's behaviors programmatically, and there I hit a wall. ProtoBuf.ServiceModel.ProtoEndpointBehavior, available on .NET 3.0 implementation of protobuf-net is not available on the iOS release.

How would I go about using protobuf-net to deserialize objects received from a windows-based WCF endpoint using protobuf-net serialization.

like image 881
Louis Boux Avatar asked Sep 29 '11 18:09

Louis Boux


1 Answers

It is actually pretty much the same as described in this blog entry by Friction Point Studios. Since meta-programming on the device is not really an option, the trick is to pre-generate a serialization dll. This can be done by creating a small console exe (this is just a tool - it isn't designed to be pretty) that configures a RuntimeTypeModel (by adding the types you are interested in), and then call .Compile(...):

var model = TypeModel.Create();
model.Add(typeof (ProtoObject), true);
model.Compile("MySerializer", "MySerializer.dll");

This generates a serializer dll; simply reference this dll (along with the iOS version protobuf-net), and use the serializer type in the dll to interact with your model:

var ser = new MySerializer();
ser.Serialize(dest, obj); // etc
like image 67
Marc Gravell Avatar answered Oct 14 '22 11:10

Marc Gravell