Skip to content

How to create a blank MessagePackSerializerOptions instance without standard resolver? #701

Closed

Description

@kpto

I need to create a MessagePackSerializerOptions with GeneratedResolver. It seems that I can only create one by accessing the static Standard property which tries to initialize a standard resolver. But standard resolver contains dynamic resolvers which are not supported by Xamarin and System.PlatformNotSupportedException was thrown.

Activity

  1. AArnott commented on Dec 4, 2019

    @AArnott
    Collaborator

    Interesting. So even the initialization of the static Standard instance throws an exception on these platforms? Can you share a callstack? That sounds like something we need to fix.

    There's no such thing as MessagePackSerializerOptions without a resolver, but evidently we need to provide a way for you to get/create an instance that conforms to your platform.

    Maybe we'll change Standard to not throw on these platforms. Or maybe we can add a static Create factory method on the type to allow you to specify the resolver.

  2. added this to the v2.0 milestone on Dec 4, 2019
  3. self-assigned this
    on Dec 4, 2019
  4. AArnott commented on Dec 4, 2019

    @AArnott
    Collaborator

    This is a duplicate of #159 since both Xamarin.iOS and UWP when compiled with .NET Native do not support Ref.Emit, which is where the PlatformNotSupportedException is coming from.

  5. neuecc commented on Dec 5, 2019

    @neuecc
    Member

    The practice in this case is to use CompositeResolver.Create.
    So when you compose with StandardResolver, the type that raises PlatformNotSupportedException has been handled by the previous GeneratedResolver.
    If you are still unsatisfied, you can add the sub resolvers of StandardResolver with CompositeResolver.Create.

  6. AArnott commented on Dec 5, 2019

    @AArnott
    Collaborator

    @neuecc The problem isn't creating the resolver. It's getting an instance of MessagePackSerializationOptions to set the resolver on. The only obvious one is MessagePackSerializationOptions.Standard, which throws on these platforms.

    @kpto for now I suspect using PrimitiveObjectResolver.Options as your basis and changing out the resolver on it will work. But that doesn't seem discoverable IMO.

  7. neuecc commented on Dec 6, 2019

    @neuecc
    Member

    Thought that PlatformNotSupportedException will issued when touched StandardResolver.GetFormatter<T> that T is user define type.

    But, ah, okay.
    StandardResolver.Resolvers uses DynamicObjectResolver.Instance
    that create new DynamicAssembly and it uses AssemblyBuilder.DefineDynamicAssembly immediately.
    Well, this is different from what I thought and it is not good behavior.

  8. kpto commented on Dec 6, 2019

    @kpto
    Author

    Just found that there are preprocessor directive trying to exclude dynamic resolvers from the StandardResolver, but the spelling of the .NetStandard symbol is wrong so they are not excluded. I have created pull request #705 to correct the spelling. I have deployed it to both simulator and real iPad and it works.

    Should be NETSTANDARD2_0 instead of NET_STANDARD_2_0 according to Microsoft doc:
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

    This is the first time I contribute open source project, I hope I have done it correctly and politely.

  9. kpto commented on Dec 6, 2019

    @kpto
    Author

    Okay, I think I did it wrong, correcting the spelling ruins the test because that the test running in .NetFramework use the library targeting .NetStandard, and all tests that test dynamic resolvers failed.

    I don't understand why some code is excluded from .NetStandard2.0 when the library is targeting .NetStandard2.0.

    If NET_STANDARD_2_0 is not a misspell but a custom symbol instead, I don't see there is any .csproj defining it as well when I try to grep all files that contains this string, except that it appears in ./src/MessagePack.UnityClient/code_generate.bat

    I think this problem is beyond my knowledge, sorry about that, I am new in programming.

  10. AArnott commented on Dec 6, 2019

    @AArnott
    Collaborator

    It's very odd, I agree. But NET_STANDARD_2_0 is set by Unity when in the unity build, which is not represented in any csproj file. The "misspelling" is useful though because it means we can detect when it's Unity's version of netstandard2.0 which is not quite the same thing as netstandard2.0 itself.

    The proper fix to this may be #703. I'm awaiting @neuecc's review of that before finishing it.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions