Where is Serializer on .net core ProtoBuf?

Viewed 805

I using Protobuf in Dotner Core 3.1 and install this Package on my project, but when i write these line of codes for serializing requested object :

 public static byte[] ProtoSerialize<T>(T record) where T : class
    {
        try
        {
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, record);
                return stream.ToArray();
            }
        }
        catch
        {
            throw;
        }
    }

Visual studio can not find related name space and got error that can not find Serializer,

The name 'Serializer' does not exist in the current context

I test it on .Net and install related package that works but in .Net Core i got error. What is related namespece ? what is the problem?

1 Answers

Basically, install the protobuf-net package.

protobuf-net.Core is not "protobuf-net on .NET Core" - rather it is "the core part of protobuf-net". protobuf-net takes a dependency on protobuf-net.Core, regardless of whether you're using .NET Framework, .NET Core, .NET 5+, or something else (on .NET Standard).

  • protobuf-net contains all of the runtime emit logic, in addition to the Serializer class
  • protobuf-net.Core contains the main reader/writer logic

There are scenarios where you don't want or need the runtime emit pieces, but in your case: you do - so just change your package dependency to protobuf-net.

The issue of which platform you are targeting is handled by that package manager - you don't need to worry about that. It will install the correct binaries for your platform.

Related