Forum Xamarin.Forms

Using Protocol Buffer in Xamarin

I'm trying to implement Protocol Buffers in a test app, and I'm running into problems.

I'm trying to use a NuGet package called Xamarin.Forms.Labs.Services.Serialization.ProtoBuf.

It seems that this library is not PCL-compatible, so I have to include it into my Droid and iOS projects rather than my Common ones. I can work with that using Dependency Injection.

The problem is that all objects to be serialized must use attributes from the ProtoBuf-net package, which means the model objects must also be dependency injected. This seems like a really terrible solution.

Is there any way to use this library in a Xamarin project without dependency-injecting every object type?


  • DanielLDanielL PLInsider ✭✭✭✭

    It is possible to use proto-buf without declaring attributes:

    TypeModel.Default.Add(typeof(Product), false).Add("ProductName", "ProductId");

    You can also use some alternative libraries like MessagePack or Migrant

  • JohnBrinkJohnBrink USMember
    edited June 2014

    I see a TypeModel, but no TypeModel.Default. It looks like I can call that method on TypeModel.Create(), but how do I apply the new type to the ProtoBuf serializer?

    Edit: This still requires accessing the ProtoBuf library, so I'd have to have code to describe the type (in the PCL) in the app project. Or to go back to dependency-injecting the type from the app.

  • JohnBrinkJohnBrink USMember

    Update: I found Default under RuntimeTypeModel. I got it to work without dependency-injecting the model types. My current solution is a bit messy, as it involves calling the method you gave me from each object's static constructor. However, this is a big step up from what I had before.

  • DanielLDanielL PLInsider ✭✭✭✭

    Sorry, It has to be RuntimeTypeModel.Default.Add method, just google it ;)

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    Hi @JohnBrink‌ if you facing some problems or issues feel free to add a issue to the Xamarin Forms Labs project on github .

    maybe @SKall‌ can give you a hand on this one!

  • SKallSKall USMember ✭✭✭✭
    edited June 2014

    @JohnBrink, the package is PCL. Maybe you tried to add it to a library that was not using Profile78? Not all the objects need attributes, only the ones that are complex that ProtoBuf cannot resolve (like collections). For classes containing primitive types all you need is DataContract with ordered DataMember attributes and those are in the BCL, not in ProtoBuf.

  • EgorbEgorb USMember

    In my CrossChat sample I am using Protobuf without attributes (it creates model for all types in special DTO assembly)

  • JohnBrinkJohnBrink USMember

    @SKall: Thank you so much! I don't know why I couldn't install it to my PCL before, but it's all working now (using the DataContract/DataMember attributes).

Sign In or Register to comment.