二进制流“0”不包含有效的 BinaryHeader。随机发生

2023-12-05

我正在开发处理 firebird 数据库请求的 c# windows 服务。当我尝试在客户端应用程序上反序列化对象时,我的问题随机发生(有时在 5 分钟后,有时在仅 4 次数据库调用后)。它只发生在特定位置(停止在 54 字节数组中的第 18 个字节)。其余时间该函数返回正确的结果。


我正在使用这个函数来序列化单个对象

public byte[] ObjectToByteArray(Object obj)
{
    if (obj == null)
        return null;
    MemoryStream fs = new MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(fs, obj);
    fs.Seek(0, SeekOrigin.Begin);
    byte[] rval = fs.ToArray();
    fs.Close();
    return rval;
}

我没有序列化任何自定义类,仅序列化字符串和数字类型(尽管 firebird api 将它们作为对象返回)。 我用它来反序列化:

public object ByteArrayToObject(Byte[] Buffer)
{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream(Buffer);
    stream.Position = 0;
    object rval = formatter.Deserialize(stream); <--- this thing drives me nuts.
    stream.Close();
    return rval;
}

以及客户端应用程序中的主要功能。抱歉代码丑陋,

    public List<object[]> ByteToList(byte[] data, int[] pomocnicza)
    {
        //pomocnicza table contains size of (original) particular column of list in bytes
        int size_row = 0;
        foreach (int i in pomocnicza)
        { size_row += i; }
        List<object[]> result = new List<object[]>();
        int iterator = 0;
        for (int i = 0; i < data.Length / size_row ; i++)
        {
            object[] zxc = new object[3];
            int l = pomocnicza.Length/4;
            for (int j = 0; j < l; j++)
            {
                byte[] tmp = new byte[pomocnicza[j*4]];
                System.Array.Copy(data, iterator, tmp, 0, pomocnicza[j*4]);
                object ffs = ByteArrayToObject(tmp);
                zxc[j] = ffs;
                iterator += pomocnicza[j*4];
            }
            result.Add(zxc);
        }
        return result;
    }

令我困惑的是,它在大多数情况下都有效,但不可避免地会导致抛出错误。它随机发生的事情使得精确定位变得更加困难。请帮忙。


@编辑 这就是我读取输入的方式:

    public List<object[]> RetrieveSelectData(FbConnection dbConn, string SQLCommand)
    {
        using (var command = dbConn.CreateCommand())
        {
            command.CommandText = SQLCommand;
            using (var reader = command.ExecuteReader())
            {
                var rows = new List<object[]>();
                while (reader.Read())
                {
                    var columns = new object[reader.FieldCount];
                    reader.GetValues(columns);
                    rows.Add(columns);
                }
                return rows;
            }
        }
    }

然后用这个函数序列化

    public byte[] ListToByte(List<object[]> lista, out int[] rozmiary)
    {
        int size= 0;
        rozmiary = new int[lista[0].Length];
        for (int i = 0; i < lista[0].Length; i++)
        {
            byte[] test = this.ObjectToByteArray(lista[0][i]);
            size+= test.Length;
            rozmiary[i] = test.Length;
        }
        size*= lista.Count;
        byte[] result = new byte[size];
        int index = 0;
        for (int i = 0; i < lista.Count; i++)
        {
            for (int j = 0; j < lista[i].Length; j++)
            {
                byte[] tmp = this.ObjectToByteArray(lista[i][j]);                  
                tmp.CopyTo(result, index);
                index += tmp.Length;
            }
        }
        return result;
    }

如果您正在使用上述反序列化方法,并且在从 clientstream 或其他流获取流时也调用它们......跳过它。尝试直接使用带有格式化程序的这些流。像下面这样:

NetworkStream clientStream = client.GetStream();
Object src = (Object)formatter.Deserialize(clientStream);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

二进制流“0”不包含有效的 BinaryHeader。随机发生 的相关文章

随机推荐