Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.29k stars 193 forks source link

MemoryPack.MemoryPackSerializationException : <class> is not registered in this provider #178

Closed g3rzi closed 12 months ago

g3rzi commented 1 year ago

There was already issue about that:
https://github.com/Cysharp/MemoryPack/issues/119

The solution was to go to the csproj file and change from MemoryPack to MemoryPack.Core.
Unfortunately, my csproj has only MemoryPack and it still doesn't work, this is why I opened a new case.

I installed MemoryPack from the documentation:

NuGet\Install-Package MemoryPack -Version 1.9.16

Also Roslyn:

NuGet\Install-Package Microsoft.Net.Compilers -Version 4.2.0

I have Visual Studio 2022 17.7.3, I am using the example from the documentation:

using MemoryPack;

namespace ConsoleApp1
{
    [MemoryPackable]
    public partial class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var v = new Person { Age = 40, Name = "John" };
            var bin = MemoryPackSerializer.Serialize(v);
            var val = MemoryPackSerializer.Deserialize<Person>(bin);
            Console.WriteLine(val);
        }
    }
}

And I am getting the same error:

Hello, World!
Unhandled exception. MemoryPack.MemoryPackSerializationException: ConsoleApp1.Person is not registered in this provider.
   at MemoryPack.MemoryPackSerializationException.ThrowNotRegisteredInProvider(Type type)
   at MemoryPack.ErrorMemoryPackFormatter`1.Throw()
   at MemoryPack.ErrorMemoryPackFormatter`1.Serialize[TBufferWriter](MemoryPackWriter`1& writer, T& value)
   at MemoryPack.MemoryPackSerializer.Serialize[T](T& value, MemoryPackSerializerOptions options)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\myuser\source\repos\MemoryPackConsole1\MemoryPackConsole1\Program.cs:line 27

My csproject file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MemoryPack" Version="1.9.16" />
    <PackageReference Include="Microsoft.Net.Compilers" Version="4.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

I saw that in the exception that it still uses the MemoryPack.core:
image

Also here: image

Here the code generated:
image

shishiraltysys commented 12 months ago

Hello there,

Have you discovered any answers to this problem? This is the bottleneck for us, and we need a solution. Could you please make this a priority?

Thanks in advance.

g3rzi commented 12 months ago

Unfortunately, I haven't found it yet, I hope the owner @neuecc will help us because this issue is not solved.

shishiraltysys commented 12 months ago

Hello @neuecc,

Could you please help us.

neuecc commented 12 months ago

Can you remove Microsoft.Net.Compilers ? It hookss special compiling process, it harms the generator.

neuecc commented 12 months ago

If you want to use Microsoft.Net.Compilers, It seems to work with <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.8.0-2.final">.

shishiraltysys commented 12 months ago

Hello @neuecc

Thanks for your response, but i haven't used Microsoft.Net.Compilers in my application. I have used below dependencies.

image

neuecc commented 12 months ago

It seems that the situation with g3rzi is different. Please provide more details about your situation (csproj, Version of IDE (Visual Studio), generated code).

g3rzi commented 12 months ago

@neuecc thank you, it works!

after going to Packages and removing the Microsoft.Net.Compilers it worked:
image

Also, the second solution to edit the MemoryPackConsole1.csproj file works too:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="MemoryPack" Version="1.9.16" />
        <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.8.0-2.final">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
    </ItemGroup>

</Project>