Deserialize an object in .NET Core 7.0 which was serialized in .NET 4.8

247 Views Asked by At

Edits

Due to the (justified) comments about my question lacking code and clarity, I've added some samples hereunder, however I can't really provide a MWE due to Cognex's vision pro commercial license. Also I didn't make it clear: I can't change the code behind the classes I need to deserialize.

Context

I am trying to use a commercial image analysis library (Cognex VisionPro) which is written in .NET 4.8 in a C# Blazor application (server-side hosted, .NET Core 7.0). The commercial library has UI components which we don't use in Blazor.

I need to

  1. Serialize a file in a .NET 4.8 application (commercial, I can't change it)
  2. Deserialize this file in my .NET Core 7.0 application

My issue

When deserializing the file (see code hereunder), I get the error

// this triggs the error hereunder. I cannot change the code for neither CogToolBlock nor CogSerializer classes.
// reminder, this code is in donetcore 7.0
CogToolBlock toolblock = CogSerializer.LoadobjectFromFile(filePath);

Could not load file or assembly 'System.Runtime.Serialization.Formatters.Soap, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

The underlying issue is that binary formatters are obsolete in .NET Core.

What I tried

  • I saw in this github issue that using CoreFX should work but in my case it didn't solve the issue (probably because the object I'm trying to deserialize contains lots of complex types).

  • Also, I saw in MSDN documentation that I could still use binary formatters with <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization> but I had no luck with this. My idea was to build a small .NET 4.8 app, instantiate an object of the class I'm interested (CogToolBlock) and serialize it using BinaryFormatter, see the code hereunder:

// this whole sequence is in a .NET 4.8 app
var toolblock = CogSerializer.LoadObjectFromFile(@"C:\tmp\sequence.vpp"); 
using (var fs= new FileStream(@"C:\tmp\sequence.vpp.dat", FileMode.Create))
{
    var formatter = new BinaryFormatter();
    formatter.Serialize(fs, Serialize(toolblock));
}

\\ some more code...

\\ the method to translate my instance to an array of bytes
private static byte[] Serialize(CogToolBlock obj)
{
    using (var memoryStream = new MemoryStream())
    {
        var binaryFormatter = new BinaryFormatter(); // <-- notice I'm using BinaryFormatter
        binaryFormatter.Serialize(memoryStream, obj);
        return memoryStream.ToArray();
    }
}

The above code sequence works and I end up with a "sequence.vpp.dat" which has the same size as my "sequence.vpp" original file. The problem arises when I'm trying to deserialize this saved file in my Blazor app.

byte[] fileBytes = File.ReadAllBytes(@"C:\tmp\sequence.vpp.dat");
CogToolBlock = Deserialize(fileBytes); 

// ... some code

private static CogToolBlock Deserialize(byte[] data)
{
    using (var memoryStream = new MemoryStream(data))
    {
        var binaryFormatter = new BinaryFormatter();
        return binaryFormatter.Deserialize(memoryStream) as CogToolBlock; // <-- ERROR here
    }
}

As per @dbc's comment, sure it's been obsolete a long time, but if I had to use it and MSDN says there's a way, it would make sense to try it.

  • I'm trying to use google-protobuf, but I can't serialize the complex object (in a desktop .NET application) because :

    No serializer defined for type: Cognex.VisionPro.ToolBlock.CogToolBlock'

Any idea or pointer would be a great help.

0

There are 0 best solutions below