Code Monkey home page Code Monkey logo

Comments (3)

neuecc avatar neuecc commented on September 27, 2024

Thank you.
To fully apply Native AOT, a fundamental understanding may be necessary.
Currently, we're only applying stopgap measures.

from memorypack.

SerratedSharp avatar SerratedSharp commented on September 27, 2024

This also occurs in a Blazor WebAssembly Standalone project. I have attached a minimal reproduction project in .NET 8.

BlazorAppMemoryPackBug.zip

This may be more related to trimming than it is AOT. There are attributes such as the DynamicDependencyAttribute that can be applied to types to flag them as dynamically accessible, but it gets pretty nuanced. I'm not clear on what is trying to be accessed dynamically that is failing. From the stack trace it appears to try and call a Constructor ctor on the interface IBob?

What is going on here that it is trying to call a constructor on the interface? Is this normal behavior for MemoryPack, or would it be expected that it would normally identify the underlying derived concrete type and try to construct it instead?

at BlazorAppMemoryPackBug.IBob..cctor()

You can set a breakpoint in the catch block, or open the browser debug console as this is where WebAssembly Console.WriteLine outputs to:
image

MemoryPack.MemoryPackSerializationException: BlazorAppMemoryPackBug.IBob is failed in provider at creating formatter.
blazor.webassembly.js:1  ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
blazor.webassembly.js:1  ---> System.TypeInitializationException: The type initializer for 'BlazorAppMemoryPackBug.IBob' threw an exception.
blazor.webassembly.js:1  ---> System.BadImageFormatException: Method has no body
blazor.webassembly.js:1 File name: 'MemoryPack.Core'
blazor.webassembly.js:1    at MemoryPack.MemoryPackFormatterProvider.Register[IBob]()
blazor.webassembly.js:1    at BlazorAppMemoryPackBug.IBob..cctor() in C:\Users\Aaron\source\repos\BlazorAppMemoryPackBug\obj\Debug\net8.0\MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\BlazorAppMemoryPackBug.IBob.MemoryPackFormatter.g.cs:line 31
blazor.webassembly.js:1    --- End of inner exception stack trace ---
blazor.webassembly.js:1    at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
blazor.webassembly.js:1    at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
blazor.webassembly.js:1    --- End of inner exception stack trace ---
blazor.webassembly.js:1    at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
blazor.webassembly.js:1    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
blazor.webassembly.js:1    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
blazor.webassembly.js:1    at MemoryPack.MemoryPackFormatterProvider.TryInvokeRegisterFormatter(Type type)
blazor.webassembly.js:1    at MemoryPack.MemoryPackFormatterProvider.Cache`1[[BlazorAppMemoryPackBug.IBob, BlazorAppMemoryPackBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]..cctor()
blazor.webassembly.js:1    --- End of inner exception stack trace ---
blazor.webassembly.js:1    at MemoryPack.MemoryPackSerializationException.ThrowRegisterInProviderFailed(Type type, Exception innerException)
blazor.webassembly.js:1    at MemoryPack.ErrorMemoryPackFormatter`1[[BlazorAppMemoryPackBug.IBob, BlazorAppMemoryPackBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Throw()
blazor.webassembly.js:1    at MemoryPack.ErrorMemoryPackFormatter`1[[BlazorAppMemoryPackBug.IBob, BlazorAppMemoryPackBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Serialize[ReusableLinkedArrayBufferWriter](MemoryPackWriter`1& writer, IBob& value)
blazor.webassembly.js:1    at MemoryPack.MemoryPackSerializer.Serialize[IBob](IBob& value, MemoryPackSerializerOptions options)
blazor.webassembly.js:1    at BlazorAppMemoryPackBug.Program.Main(String[] args) in \source\repos\BlazorAppMemoryPackBug\Program.cs:line 30

from memorypack.

SerratedSharp avatar SerratedSharp commented on September 27, 2024

I was able to workaround this by registering programmatically with MemoryPackFormatterProvider.Register(

@carmichaelalonso Does this approach work in your case?

[MemoryPackable]
public partial class Bob: IBob
{
    public string Name { get; set; }
}

[MemoryPackable]
[MemoryPackUnion(0, typeof(Bob))]
public partial interface IBob
{
    string Name { get; set; }
}

public class Program
{
    public static async Task Main(string[] args)
    {
        var formatter = new DynamicUnionFormatter<IBob>(
            (0, typeof(Bob))
        );
        MemoryPackFormatterProvider.Register(formatter);

        var bob = new Bob() { Name = "Bob"};
        var ibob = (IBob)bob;
        MemoryPackSerializer.Serialize(ibob);

from memorypack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.