Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

How to use Protobuf-net in Xamarin Forms?

$
0
0

I am using protobuf-net in my current Xamarin Forms project and installed it with nuget into my PCL project.
When I build the project for Android in Release mode and run it, I get the following exception:

12-09 13:00:30.829 I/MonoDroid( 1489): UNHANDLED EXCEPTION:
12-09 13:00:30.849 I/MonoDroid( 1489): System.TypeLoadException: Could not load type FooApp.Services.Model.PageParameter
1[[System.String, System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]] while decoding custom attribute 12-09 13:00:30.849 I/MonoDroid( 1489): at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool) 12-09 13:00:30.849 I/MonoDroid( 1489): at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType, Boolean inheritedOnly) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at System.MonoType.GetCustomAttributes (System.Type attributeType, Boolean inherit) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, System.Type type, Boolean inherit) [0x00000] in :0 An unhandled exception occured. 12-09 13:00:30.849 I/MonoDroid( 1489): at System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, Boolean inherit) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at System.Reflection.CustomAttributeExtensions.GetCustomAttributes (System.Reflection.MemberInfo element, Boolean inherit) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at ProtoBuf.Meta.AttributeMap.Create (ProtoBuf.Meta.TypeModel model, System.Type type, Boolean inherit) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at ProtoBuf.Meta.MetaType.GetContractFamily (ProtoBuf.Meta.RuntimeTypeModel model, System.Type type, ProtoBuf.Meta.AttributeMap[] attributes) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at ProtoBuf.Meta.RuntimeTypeModel.TryGetBasicTypeSerializer (System.Type type) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto (System.Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled) [0x00000] in :0 12-09 13:00:30.849 I/MonoDroid( 1489): at ProtoBuf.Meta.RuntimeTypeModel.GetKey (System.Type type, Boolean demand, Boolean getBaseKey) [0x00000] in :0 12-09 13:00:30.859 I/dalvikvm( 1489): Could not find method java.lang.Throwable., referenced from method android.runtime.JavaProxyThrowable. 12-09 13:00:30.859 W/dalvikvm( 1489): VFY: unable to resolve direct method 5628: Ljava/lang/Throwable;. (Ljava/lang/String;Ljava/lang/Throwable;ZZ)V 12-09 13:00:30.859 D/dalvikvm( 1489): VFY: replacing opcode 0x70 at 0x0000 12-09 13:00:31.570 E/mono ( 1489): 12-09 13:00:31.570 E/mono ( 1489): Unhandled Exception: 12-09 13:00:31.570 E/mono ( 1489): System.TypeLoadException: Could not load type FooApp.Services.Model.PageParameter
1[[System.String, System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]] while decoding custom attribute
12-09 13:00:31.570 E/mono    ( 1489):   at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool)
12-09 13:00:31.570 E/mono    ( 1489):   at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType, Boolean inheritedOnly) [0x00000] in <filename unknown>:0 
12-09 13:00:31.570 E/mono    ( 1489):   at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
The program 'Mono' has exited with code 0 (0x0).

The PageParameter class looks like this (This class is only used in the PCL project):
[ProtoContract] 
[ProtoInclude(2, typeof(PageParameter))] 
[ProtoInclude(3, typeof(PageParameter))]
[ProtoInclude(4, typeof(PageParameter))] 
[ProtoInclude(5, typeof(PageParameter))]
[ProtoInclude(6, typeof(PageParameter))] 
[ProtoInclude(7, typeof(PageParameter))] 
[ProtoInclude(8, typeof(PageParameter))] 
public abstract class PageParameter {
    public abstract object UntypedValue { get; set; }
    public static PageParameter<T> Create<T>(T value)
    {
        return new PageParameter<T> { Value = value };
    }
    public static PageParameter CreateDynamic(object value)
    {
        if (value is int)
        {
            return Create((int) value);
        }
        if(value is string)
        {
            return Create((string) value);
        }
        if (value is IAlarmProvider)
        {
            return Create((IAlarmProvider)value);
        }
        if (value is AlarmProviderType)
        {
            return Create((AlarmProviderType)value);
        }
        if (value is SearchType)
        {
            return Create((SearchType)value);
        }
        if (value is Approval)
        {
            return Create((Approval)value);
        }
        if (value is WorklogViewModel)
        {
            return Create((WorklogViewModel)value);
        }
        var param = (PageParameter)Activator.CreateInstance(
            typeof(PageParameter<>).MakeGenericType(value.GetType()));
        param.UntypedValue = value;
        return param;
    }
}

[ProtoContract]
public sealed class PageParameter<T> : PageParameter
{
    [ProtoMember(1)]
    public T Value { get; set; }
    public override object UntypedValue
    {
        get { return Value; }
        set { Value = (T)value; }
    }
    public override string ToString()
    {
        return Value.ToString();
    }
}

As I using the protobuf-net library in the PCL project and not in the Android project, the Preserve Attribute is not available. I have also tried to get it to work with the help of a LinkerPleaseInclude .cs file but without success.

Note: The release build is working when I build the app with Linker option set to "None" but the size of the APK file is then 36MB. I would like to avoid such a big file and use the "SDK Assemblies only" option.

Any ideas?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>