1

I have 2 programs, one written in .Net Framework 4.7.x and another app that is dot net core 2.1.

Program 1, written in .Net Framework. takes a Dataset and using BinaryFormatter writes it to a database field as a byte[].

Program 2, written in dot net core, then takes that record and attempts to Deserialize the field using BinaryFormatter. I'm getting an exception "ArgumentException: Type 'System.Byte' is not deserializable."

Program 1

static void Main(string[] args)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["CDPMetadataModelContext"].ConnectionString;
        byte[] binaryFormattedDs;
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (var da = new SqlDataAdapter("Select * from Code_Job_Status", connection))
            {
                var ds = new DataSet();
                da.Fill(ds);
                binaryFormattedDs = SerializeBinaryDataContract(ds);
            }
            string query = "INSERT INTO dbo.Result_Cache (Result_Binary_Data, Run_DateTime) output inserted.Result_Cache_ID VALUES";
            string insertQuery = "(@binaryValue, getdate())";
            using (SqlCommand cmd = new SqlCommand(query + insertQuery, connection))
            {
                cmd.Parameters.AddWithValue("@binaryValue", SqlDbType.VarBinary).Value = binaryFormattedDs;                   
                var newId = (int)cmd.ExecuteScalar();
                Console.WriteLine("New Id= "+ newId);
                Debug.WriteLine("New Id= " + newId);
            }
            connection.Close();
        }

        Console.ReadKey();
        var ds2 = DeSerialize<DataSet>(binaryFormattedDs);
        Console.WriteLine("Row Count=" + ds2.Tables[0].Rows.Count);
        Console.ReadKey();
    }

    public static T DeSerialize<T>(byte[] bytes)
    {
        var serializer = new BinaryFormatter();
        using (var ms = new MemoryStream())
        {
            ms.Write(bytes, 0, bytes.Length);
            ms.Seek(0, SeekOrigin.Begin);
            return (T)serializer.Deserialize(ms);
        }

    }

    public static byte[] SerializeBinaryDataContract(DataSet dataSet)
    {

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            byte[] buffer;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                dataSet.RemotingFormat = SerializationFormat.Binary;
                binaryFormatter.Serialize((Stream)memoryStream, (object)dataSet);
                buffer = new byte[memoryStream.Length];
                memoryStream.Seek(0L, SeekOrigin.Begin);
                memoryStream.Read(buffer, 0, buffer.Length);
            }
            return buffer;

    }

Program 2

 static void Main(string[] args)
    {


        Console.WriteLine("Result_Cache_ID:");
        var reportIds = Console.ReadLine();
        if (int.TryParse(reportIds, out var resultCacheId))
        {

            GetDS(resultCacheId);
        }


        Console.ReadKey();
    }

    private static void GetDS(int resultCacheId)
    {

        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.Open();

            using (var da = new SqlDataAdapter("Select * from Result_Cache where Result_Cache_ID="+ resultCacheId, connection))
            {
                var ds = new DataSet();
                da.Fill(ds);
                var binaryFormattedDs =(byte[]) ds.Tables[0].Rows[0]["Result_Binary_Data"];
                var ds2 = DeSerialize<DataSet>(binaryFormattedDs);
                Console.WriteLine("Row Count=" + ds2.Tables[0].Rows.Count);
            }

        }

    }

    public static T DeSerialize<T>(byte[] bytes)
    {
        var serializer = new BinaryFormatter();
        using (var ms = new MemoryStream())
        {
            ms.Write(bytes, 0, bytes.Length);
            ms.Seek(0, SeekOrigin.Begin);
            return (T)serializer.Deserialize(ms);
        }

    }

am I doing something wrong?

Brian
  • 1,845
  • 1
  • 22
  • 37
  • I believe the `BinaryFormatter` serializer was intended to be serialized and deserialized on the same system. I'm also pretty sure it was deprecated in .NET Core as well. See https://stackoverflow.com/a/703361/507793 – Matthew Feb 26 '19 at 19:36
  • BinaryFormatter, is supported in 2.1 https://learn.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization – Brian Feb 26 '19 at 19:48
  • also tracking on Microsofts CoreFx forum on github https://github.com/dotnet/corefx/issues/35629 – Brian Feb 28 '19 at 15:00

0 Answers0