开发者

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.


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


Just to bring this up to date there are a few issues with using WCF + Protobuf on MonoTouch. As you have observed the current releases of System.ServiceModel and protobuf light for ios don't include all the necessary bits.

However if you go and get the full System.ServiceModel from the Mono repository on GitHub and build it against the full Protobuf source then you can get it to work; I have done so.

You need to generate a serialisation assembly using the precompile tool then edit the ProtoOperationBehavior attribute to give it some way to reference your serialisation assembly. All the changes are too extensive to document here but it can be done and it is a lot faster than DatacontractSerializer which is pretty awful on iOS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜