Binary deserialization

Created at 19 Jan 2024, 08:52
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CO

courquinpasisa

Joined 24.08.2018

Binary deserialization
19 Jan 2024, 08:52


Hello,
I have a problem with binary deserialization of an object in an Algo.
I reproduced the problem by simplifying the code as much as possible and inserting the object's class into the Algo.


I manage to serialize my object into a binary file, but when I want to deserialize it, I always get the following message:
'Crashed in OnStart with SerializationException: Unable to find assembly 'TestSerializ, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.


This code works perfectly in a console application but not in cAlgo, I don't understand where the error comes from.

Target framework: .NET 6.0

I've found that this code works if I choose : Target framework: .NET framework 4.x
but I'm using several classes already developed in .NET 6 

Here's the code:
 

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class TestSerializ : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            // Serialisation Objet
            string cheminFileTest = "ObjetTest.bin";
            ObjetTest obj1 = new("Hello", "azerty", 54, 96);
            Save(obj1, cheminFileTest);

            Thread.Sleep(5000);

            // Deserialisation Objet
            ObjetTest obj2 = Open(cheminFileTest);
            Print(obj2.value3); 
            Print(obj2.value6.ToString());
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }

        public void Save(ObjetTest obj, string filePath)
        {
            FileStream fs = File.Create(filePath);
            BinaryFormatter bf = new ();
            bf.Serialize(fs, obj);
            fs.Close();
        }

        public ObjetTest Open(string filePath)
        {
            FileStream fs = File.OpenRead(filePath);
            BinaryFormatter bf = new ();
            ObjetTest obj = (ObjetTest)bf.Deserialize(fs);
            return obj;
        }
    }

    [Serializable]
    public class ObjetTest
    { 
        public string value1;
        public string value2;
        public string value3;
        public int value4;
        public int value5;
        public int value6;

        public ObjetTest(string v1, string v2, int v5, int v6)
        {
            value1 = v1;
            value2 = v2;
            value3 = v1 + " ----> " + v2;

            value4 = v5;
            value5 = v6;
            value6 = v5 + v6;

        }
    }
}

Thanks in advance.
Sorry, if my English isn't good...

 


@courquinpasisa