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.