2

I have a class library that has a class marked as [Serializable]. If I reference the project directly, I can serialize the class using BinaryFormatter:

 BinaryFormatter bf = new BinaryFormatter();
 MemoryStream ms = new MemoryStream();
 bf.Serialize(ms, obj);
 var data = ms.ToArray();

However, If I publish the class library through nuget, it loses the [Serializable] attribute and can no longer be converted to a byte array using the BinaryFormatter.

Is there a way to preserve the class AS-IS when you create the .nupkg? Currently, I'm just using Visual Studios Generate Nuget Package on build.

After further investigation let me clarify my findings.

*If you have a .net472 Web API and a .net472 class library and you publish the class library through nuget everything works. I have used BinaryFormatter to serialize an object that is marked with the [Serializable] attribute and send it to the server successfully and was able to deserialize it.

*If you have a .netstandard 2.0 library, and you publish it out to a .netstandard console project and attempt to serialize it with the binaryformatter you get the class is not marked as serializable (even though it's marked).

*If you have a .net472 library and you publish it out to a netstandard 2.0 console application, I was unable to serialize the class but it was for other reasons, in the case I had I received error:

Type 'System.Text.Encoding' in Assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' is not marked as serializable.'

In short, if you really need binary serialization, I don't know of any other option but for both the client and the server to support the full framework library and not a core/netstandard.

Community
  • 1
  • 1
Watson
  • 1,385
  • 1
  • 15
  • 36
  • Look through [this](https://stackoverflow.com/questions/39199049/what-is-the-equivalent-of-serializable-in-net-core-conversion-projects). It looks like you may run into issues. – Kit Nov 07 '19 at 01:39
  • Thanks, I'll look at that when I get a chance but looks good. – Watson Nov 07 '19 at 01:45
  • After looking at this the problem remains. .NetCore does suppport the serializable attribute which is what the linked post you sent talks about - it was just removed and put in a separate package. The issue is, why does nuget remove the serialization that .net core allows? (separate issue.) – Watson Nov 08 '19 at 17:29
  • 1
    Just created a .net472 project. Added a class with serialization and published using nuget.exe. It strips out the serialization attributes. Any way to tell nuget not to strip them? – Watson Nov 08 '19 at 18:53

1 Answers1

1

Because .NET Standard does not define whether a type is serializable, I suspect that NuGet strips out the attribute on purpose when you target .NET Standard.

At best, it looks like NuGet is preserving the (ahem) standards of .NET Standard, but at worst, it's silently doing something you may not expect—stripping [Serializable].

So, with a bit of conjecture, it seems that the compiler doesn't care to strip the attribute or warn that it shouldn't be applied (possibly because in a multitargeting scenario, [Serializable] is allowed in some situations), but NuGet is being much more strict in your case because you're targeting only .NET Standard.

It seems like you can provide some runtime support to determine whether the type is serializable, but you can't do this at compile-time. Perhaps the only way to preserve the attribute is to target your library to .NET Core or .NET Framework.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • To clarify, both nuget published classes under net472 and netstandard 2.0 both look like the serializable attribute is removed. However, if I use binary formatter on the net472 class I can serialize it. Though, I'm just looking at the metadata assembly reference so maybe it's marked correctly in the full net472 nuget package. – Watson Nov 08 '19 at 20:28