Skip to content

Difficulties faced in using Message pack in xamarin forms #977

Closed

Description

@Nikhileshwar96

I'm trying to use Message Pack in Xamarin forms used the AOT tool too. But still, the Operation not supported in platform exception is thrown in Xamarin.Forms (iOS).

Steps followed:

  1. Installed mpc tool.
  2. Used don't mpc -i "{FormsProject.csproj path}" -o "{FormsProject directory path}"
  3. Added the created message pack generated class to the forms project.
  4. Used the following code in App.xaml.cs Initialize method
StaticCompositeResolver.Instance.Register(
     MessagePack.Resolvers.GeneratedResolver.Instance,
     MessagePack.Resolvers.StandardResolver.Instance);

Also using System.Drawing.Drawing2D.Matrix throws the following error

[MonoDroid]   at MessagePa07-10 18:06:01.269 I/MonoDroid(13495): MessagePack.MessagePackSerializationException: Failed to deserialize Project.Core.Models.SomeData Class value. ---> System.TypeInitializationException: The type initializer for 'FormatterCache`1' threw an exception. ---> MessagePack.Internal.MessagePackDynamicObjectResolverException: can't find public constructor. type:System.Drawing.Drawing2D.Matrix
[MonoDroid]   at MessagePack.Internal.ObjectSerializationInfo.CreateOrNull (System.Type type, System.Boolean forceStringKey, System.Boolean contractless, System.Boolean allowPrivate) [0x009a0] in <23c4c9b023514c20801c8f07fd69206c>:0 
[MonoDroid]   at MessagePack.Internal.DynamicObjectTypeBuilder.BuildType (MessagePack.Internal.DynamicAssembly assembly, System.Type type, System.Boolean forceStringKey, System.Boolean contractless) [0x00015] in <23c4c9b023514c20801c8f07fd69206c>:0 

Created a SO query for the same. Any help could be great.

Activity

  1. neuecc commented on Jul 13, 2020

    @neuecc
    Member

    answered at SO.
    maybe lacked this line.

    var option = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
    MessagePackSerializer.DefaultOptions = option;
  2. Nikhileshwar96 commented on Jul 14, 2020

    @Nikhileshwar96
    Author

    @neuecc

    The following line produces operation not supported on the platform exception in iOS.

    var option = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
    

    I'm using
    Xamarin.Forms version- 4.5.0.495
    MessagePack version - 2.1.152

    System.TypeInitializationException: The type initializer for 'MessagePackSerializerOptionsDefaultSettingsLazyInitializationHelper' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MessagePack.Resolvers.StandardResolver' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MessagePack.Internal.StandardResolverHelper' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MessagePack.Resolvers.DynamicEnumResolver' threw an exception. ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
      at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.pns.cs:129 
      at MessagePack.Internal.DynamicAssembly..ctor (System.String moduleName) [0x0001a] in <f04e2061de5c414991b8e36a20354820>:0 
      at MessagePack.Resolvers.DynamicEnumResolver..cctor () [0x00010] in <f04e2061de5c414991b8e36a20354820>:0 
    
  3. Nikhileshwar96 commented on Jul 15, 2020

    @Nikhileshwar96
    Author

    @AArnott Was this exception replicating in your side?
    I couldn't make MessagePack work in xamarin.forms iOS
    Or is there anything I missed?

  4. AArnott commented on Jul 16, 2020

    @AArnott
    Collaborator

    I didn't try it. I just reopened the issue since you're still working through the issue. I hope @neuecc will continue to help since I haven't really focused on AOT platforms as @neuecc has.

  5. Nikhileshwar96 commented on Jul 24, 2020

    @Nikhileshwar96
    Author

    @neuecc Is there any way I can overcome this?
    Can you suggest any steps to proceed further?

  6. Jose7777 commented on Jul 25, 2020

    @Jose7777

    @neuecc I also cannot make MessagePack work with Xamarin.Forms on iOS.
    Is there any way i can help? Any suggestion about what to see or where to start?

    Thank you in advance!

  7. neuecc commented on Aug 5, 2020

    @neuecc
    Member

    It is not a desirable behavior for the DynamicAssembly code to always be executed at initialization time.
    I've discussed this before with @AArnott ,it should be to lazy.
    Anyway, I'll impl to lazy init code.
    Other features(#if..., more target platform, etc) are after.

  8. Nikhileshwar96 commented on Aug 7, 2020

    @Nikhileshwar96
    Author

    Hi @neuecc / @AArnott ,

    Sorry for the late reply, I am a bit confused from the comment.
    Should I call the DefaultOption setting code in a lazy method? Or is it something to be taken care of on the Message Pack side.

    As much as I interpret from the comment -> DynamicAssembly code is executed at initialization all the time by message pack. And you are changing it to lazy loaded in message pack. And I have to wait for these changes.

    Am I right?

  9. AArnott commented on Aug 7, 2020

    @AArnott
    Collaborator

    @Nikhileshwar96 I don't think @neuecc was asking you to do anything. He was stating the problem, which he fixes in #996. Do you want to try out the build from that PR and see if it fixes your issue?

  10. Nikhileshwar96 commented on Aug 7, 2020

    @Nikhileshwar96
    Author

    Sure @AArnott
    How do I do that?

  11. AArnott commented on Aug 10, 2020

    @AArnott
    Collaborator

    The Azure Pipelines CI pushed a package you can try installing to see.
    https://dev.azure.com/ils0086/MessagePack-CSharp/_packaging?_a=package&feed=MessagePack-CI%40Local&package=MessagePack&protocolType=NuGet&version=2.1.165&view=overview

    That page includes a "Connect to feed" button with instructions on how to consume the package from the CI feed.

  12. Nikhileshwar96 commented on Aug 14, 2020

    @Nikhileshwar96
    Author

    Hi @AArnott / @neuecc

    Sorry for the late reply. Tried the nugget from the pipeline. This fixed the reported issue. 😃

    The fix works super nice in a simple xamarin forms app in iOS. (Both serialization and deserialization happening in the app itself)

    While in my real project app, serialization occurs in the ASP .net project and I try to deserialize here. It throws the following exception.

    Tested by serializing and deserializing the object in the app itself, it works.

    Any suggestions to overcome this?

    Thanks in advance.

    MessagePack.MessagePackSerializationException: Failed to deserialize MyProject.Core.Models.RenderImageDetails value. ---> System.TypeInitializationException: The type initializer for 'FormatterCache`1' threw an exception. ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
      at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.pns.cs:129 
      at MessagePack.Internal.DynamicAssembly..ctor (System.String moduleName) [0x0001a] in <06919c9ec1724eec910157da33d9547f>:0 
      at MessagePack.Resolvers.DynamicObjectResolver+<>c.<.cctor>b__4_0 () [0x00000] in <06919c9ec1724eec910157da33d9547f>:0 
      at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:333 
      at System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) [0x00022] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:351 
      at System.Lazy`1[T].CreateValue () [0x00074] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:431 
      at System.Lazy`1[T].get_Value () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:509 
      at MessagePack.Resolvers.DynamicObjectResolver+FormatterCache`1[T]..cctor () [0x000a1] in <06919c9ec1724eec910157da33d9547f>:0 
    
  13. AArnott commented on Aug 14, 2020

    @AArnott
    Collaborator

    Is the formatter for MyProject.Core.Models.RenderImageDetails included in the mpc generated code?

  14. Nikhileshwar96 commented on Aug 14, 2020

    @Nikhileshwar96
    Author

    Yes. it is included.
    On further examining found the following.

    The RenderImageDetails has the following property, which I didn't add for testing in simple xamarin app.

    public System.Drawing.Drawing2D.Matrix TransformationMatrix { get; set; }
    

    WIthout which the message pack is able to serialize and deserialize the class. But with the matrix property, the above exception is thrown at serialization itself. (Checked the Generated code, it has the property's formatter code).

    The error is reproduced in a simple xamarin app too (iOS).

  15. AArnott commented on Aug 15, 2020

    @AArnott
    Collaborator

    I'm going to bow out of this one and invite @neuecc to step in since he's more familiar with mpc and how it interacts with the dynamic resolvers.

  16. neuecc commented on Aug 16, 2020

    @neuecc
    Member

    How about generating a custom System.Drawing.Drawing2D.Matrix formatter first?

  17. Nikhileshwar96 commented on Aug 19, 2020

    @Nikhileshwar96
    Author

    Hi @neuecc
    I didn't try custom formatter. Since web API has to be changed too (serializatoin happens there). We are trying to eliminate the matrix property, if not required. If that doesn't solve the issue, I'll have to incorporate the custom formatter.

  18. Nikhileshwar96 commented on Aug 30, 2020

    @Nikhileshwar96
    Author

    We excluded the Transform matrix property and used the latest Message Pack version > 2.1.165. Works super fine.

    👍 Thanks for supporting in making this work.

    Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions