HavenDV / H.Pipes

A simple, easy to use, strongly-typed, async wrapper around .NET named pipes.
MIT License
219 stars 26 forks source link

MessagePack Unions not serialized correctly. #48

Closed PhilJollans closed 6 months ago

PhilJollans commented 6 months ago

Describe the bug

I have several classes defined as a Union, using the MessagePack.Union attribute. See https://github.com/MessagePack-CSharp/MessagePack-CSharp?tab=readme-ov-file#union

I had no problem with .NET 6, but with .NET 8 I find that the Union classes are not serialized correctly,

So far as I can tell, it is necessary to provide a type parameter to MessagePackSerializer.Serialize(), specifying the base class used for the union.

I have defined my own formatter class

  public class MessagePackFormatterT<T1> : FormatterBase
    where T1 : class
  {
    protected override byte[] SerializeInternal( object obj )
    {
      return MessagePackSerializer.Serialize<T1> ( obj as T1 );
    }

    protected override T DeserializeInternal<T>( byte[] bytes )
    {
      return MessagePackSerializer.Deserialize<T> ( bytes );
    }
  }

which I specify with both PipeClient and the PipeSever

_client = new PipeClient<MyBase> ( PipeName, formatter: new MessagePackFormatterT<MyBase>() ) ;
_server = new PipeServer<MyBase> ( PipeName, new MessagePackFormatterT<MyBase>() );

In my application, this seems to have fixed the problem, but I'm not sure that it is a general solution.

Steps to reproduce the bug

Define a set of classes using MessageBase.Union, following the example in https://github.com/MessagePack-CSharp/MessagePack-CSharp?tab=readme-ov-file#union

I defined the base class as an abstract class, not an interface.

Create a PipeServer an a PipeClient based on the union class.

Create an instance of one of the derived classes.

Send it via the PipeServer.

On receiving the message, the PipeClient will generate an error.

Expected behavior

No response

Screenshots

No response

NuGet package version

No response

Platform

Console

IDE

Visual Studio 2022

Additional context

.NET 8

HavenDV commented 6 months ago

Added your implementation to H.Formatters.MessagePack lib. https://github.com/HavenDV/H.Pipes/commit/a34a0a2e08c129b449c28454a9102b5153a07277

HavenDV commented 6 months ago

This is available in the latest prerelease version and will be stable after some time I don't see any other way to solve the problem, so I'm closing this.