Reputation: 1714
I'd like to share code between a .NET Framework 4.7 solution and a .NET Core solution for use in AWS Lambda.
I have created a .NET Standard 1.6 class library project and have moved some code from the .NET Framework 4.7 solution into this project in order to share it.
All is working fine except for one thing - the code is for a DTO class that is serialized by the BinaryFormatter in the .NET solution.
For example:
[Serializable]
public class BillableOptionalOperationDto
{
public string OperationDescription { get; set; }
public string Note { get; set; }
public decimal UnitPriceIncGst { get; set; }
}
I have created a polyfill so that the code compiles in the .NET Standard project.
namespace System
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate)]
public class SerializableAttribute : Attribute
{
}
}
The .NET Standard project is published as a Nuget package to our Nuget Server in TeamCity.
In the .NET Framework solution, I (quite rightly) get this error:
Error CS0433 The type 'SerializableAttribute' exists in both 'AutoGuru.Shared.Quoting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Is there a way to use the BinaryFormatter on a class in a .NET Standard 1.6 class library?
Upvotes: 1
Views: 1110
Reputation: 36583
You won't be able to serialize using a BinaryFormatter using the .NET Core runtime but you can at least cross compile using this nuget package
https://www.nuget.org/packages/System.Runtime.Serialization.Formatters/
Note though that assembly binding with .NET Standard is a total mess and, without a ton of assembly redirects, your code will compile fine but throw assembly not found exceptions at runtime. Supposedly .NET Standard 2.0 will fix that but I'm not holding my breath.
Upvotes: 1