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:
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.
answered at SO.
maybe lacked this line.
var option = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
MessagePackSerializer.DefaultOptions = option;
@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
@AArnott Was this exception replicating in your side?
I couldn't make MessagePack work in xamarin.forms iOS
Or is there anything I missed?
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.
@neuecc Is there any way I can overcome this?
Can you suggest any steps to proceed further?
@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!
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.
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?
@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?
Sure @AArnott
How do I do that?
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.
Hi @AArnott / @neuecc
Sorry for the late reply. Tried the nugget from the pipeline. This fixed the reported issue. :smiley:
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
Is the formatter for MyProject.Core.Models.RenderImageDetails included in the mpc generated code?
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).
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.
How about generating a custom System.Drawing.Drawing2D.Matrix formatter first?
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.
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.