HavenDV / H.Pipes

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

ConsoleApp sample server generates an exception with .NET 8.0 #42

Closed PhilJollans closed 9 months ago

PhilJollans commented 9 months ago

Describe the bug

I have cloned the repo and tried to run the ConsoleApp sample.
By default, .NET 8.0 is selected.

When the client connects to the server it generates the exception:

ConsoleApp.MyMessage is not registered in resolver: MessagePack.Resolvers.StandardResolver

The complete message is

Client named_pipe_test_server_021d8e5e-1e5d-4d21-9abd-4cba84e491db is now connected!
Exception: MessagePack.MessagePackSerializationException: Failed to serialize System.Object value.
 ---> MessagePack.FormatterNotRegisteredException: ConsoleApp.MyMessage is not registered in resolver: MessagePack.Resolvers.StandardResolver
   at MessagePack.FormatterResolverExtensions.Throw(Type t, IFormatterResolver resolver)
   at MessagePack.FormatterResolverExtensions.GetFormatterDynamicWithVerify(IFormatterResolver resolver, Type type)
   at MessagePack.Formatters.DynamicObjectTypeFallbackFormatter.Serialize(MessagePackWriter& writer, Object value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
   --- End of inner exception stack trace ---
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at H.Formatters.MessagePackFormatter.SerializeInternal(Object obj) in C:\GitHub\H.Pipes\src\libs\H.Formatters.MessagePack\MessagePackFormatter.cs:line 12
   at H.Formatters.FormatterBase.Serialize(Object obj) in C:\GitHub\H.Pipes\src\libs\H.Formatters\FormatterBase.cs:line 28
   at H.Pipes.PipeConnection`1.WriteAsync(T value, CancellationToken cancellationToken) in C:\GitHub\H.Pipes\src\libs\H.Pipes\PipeConnection.cs:line 162
   at ConsoleApp.MyServer.<>c__DisplayClass1_0.<<RunAsync>b__0>d.MoveNext() in C:\GitHub\H.Pipes\src\samples\ConsoleApp\MyServer.cs:line 30

The error does not occur with .NET 6.0 or .NET 7.0

Steps to reproduce the bug

  1. In the project ConsoleApp, select .NET 8.0. This is the default.
  2. Compile the solution
  3. Start the ConsoleApp and enter SERVER
  4. Start the ConsoleApp and enter CLIENT
  5. At this point the client connects to the server and the server generates an exception

Expected behavior

No response

Screenshots

No response

NuGet package version

No response

Platform

Console

IDE

Visual Studio 2022

Additional context

No response

HavenDV commented 9 months ago

I looked a little more into MessagePack, and it requires marking all types/properties with attributes, which is not entirely appropriate for Formatter by default.

[MessagePackObject]
public class MyClass
{
    // Key attributes take a serialization index (or string name)
    // The values must be unique and versioning has to be considered as well.
    // Keys are described in later sections in more detail.
    [Key(0)]
    public int Age { get; set; }

    [Key(1)]
    public string FirstName { get; set; }

    [Key(2)]
    public string LastName { get; set; }

    // All fields or properties that should not be serialized must be annotated with [IgnoreMember].
    [IgnoreMember]
    public string FullName { get { return FirstName + LastName; } }
}

So I changed this to SystemTextJsonFormatter for net8.0 and above

PhilJollans commented 9 months ago

Great, thanks for looking into this problem so quickly.

HavenDV commented 9 months ago

Now it's available in latest dev/preview version, I will release this as stable little later.