二进制流“0”不包含一个有效的BinaryHeader。随机出现 [英] Binary stream '0' does not contain a valid BinaryHeader. Occurs randomly

查看:1270
本文介绍了二进制流“0”不包含一个有效的BinaryHeader。随机出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Firebird数据库请求c#窗口服务。出现我的问题随机时刻(有时5分钟后,有时经过短短4调用数据库),当我尝试反序列化客户端应用程序对象。这种事虽然只能在特定的位置(54字节数组的第18个字节停止)。休息的时候该函数返回一个正确的结果。






我使用这个功能来序列化一个对象

 字节公众[] ObjectToByteArray(obj对象)
{
如果(OBJ == NULL)
返回NULL;
MemoryStream的FS =新的MemoryStream();
的BinaryFormatter格式=新的BinaryFormatter();
formatter.Serialize(FS,OBJ);
fs.Seek(0,SeekOrigin.Begin);
字节[] = RVAL fs.ToArray();
fs.Close();
返回RVAL;
}



我不序列化的自定义类,只有字符串和数字类型(火鸟API返回他们仿佛对象)。
我使用它来反序列化:

 公共对象ByteArrayToObject(字节[]缓冲区)
{
的BinaryFormatter格式=新的BinaryFormatter();
MemoryStream的流=新的MemoryStream(缓冲);
stream.Position = 0;
对象RVAL = formatter.Deserialize(流); < ---这件事使我坚果。
stream.Close();
返回RVAL;
}

和客户端aplication主要FNCT。对不起,丑陋的代码,

 公开名单<对象[]> ByteToList(字节[]数据,INT [] pomocnicza)
{
// pomocnicza表包含(原创)特别是在字节
INT size_row = 0列表的列的大小;
的foreach(INT我pomocnicza)
{size_row + =我; }
名单,LT;对象[]>结果=新的List<对象[]>();
INT迭代器= 0;
的for(int i = 0; I< data.Length / size_row;我++)
{
对象[] =珍香胶囊新的对象[3];
INT L = pomocnicza.Length / 4;
为(INT J = 0; J< L,J ++)
{
的byte [] TMP =新的字节[pomocnicza [J * 4];
System.Array.Copy(数据,迭代器,TMP,0,pomocnicza [J * 4]);
对象FFS = ByteArrayToObject(TMP);
ZXC [J] = FFS;
+迭代器= pomocnicza [J * 4];
}
result.Add(ZXC);
}
返回结果;
}



什么是莫名其妙我是,它在大多数情况下,但不可避免地导致抛出一个错误。东西,它发生随机品牌见真章更难。请帮忙






@EDIT
我这是怎么读取输入:

 公开名单<对象[]> RetrieveSelectData(FbConnection dbConn,字符串的SqlCommand)使用
{
(VAR命令= dbConn.CreateCommand())
{
command.CommandText =的SqlCommand; [对象]>()使用(VAR读卡器= Command.ExecuteReader却())
{
无功行=新的List<
;
,而(reader.Read())
{
VAR列=新对象[reader.FieldCount]
reader.GetValues​​(列);
rows.Add(列);
}
返回的行;
}
}
}

和再序列化使用此功能

 公共字节[] ListToByte(列表<对象[]> LISTA,OUT INT [] rozmiary)
{
INT大小= 0;
rozmiary =新INT [LISTA [0]。长度]
的for(int i = 0; I< LISTA [0]。长度;我+ +)
{
字节[] =测试this.ObjectToByteArray(LISTA [0] [I]);
尺寸+ = test.Length;
rozmiary [I] = test.Length;
}
尺寸* = lista.Count;
的byte []结果=新的字节[大小]
INT指数= 0;
的for(int i = 0; I< lista.Count;我++)
{
为(INT J = 0; J< LISTA [I]。长度; J ++)
{
的byte [] TMP = this.ObjectToByteArray(LISTA [I] [J]);
tmp.CopyTo(因此,指数);
指数+ = tmp.Length;
}
}
返回结果;
}


解决方案

我发现的bug。上面的代码工作正常,但照顾在某些情况下(!)的编码,可以随意使用它。
在一个程序中,在那里我打错并发送4个字节,但客户端应用程序被告知的另一部分铺设的问题得到8,所以在大多数情况下,它以零填充它,但有时从明年包得到它数据的。



这是@Marc Gravell和他的博客,让我过目一遍又一遍,最终找到源头。


I'm working on c# windows service that handles firebird database requests. My problem occurs at random moments (sometimes after 5 minutes, sometimes after just 4 calls to database), when I try to deserialize object on client application. It happens though only at specific position (stops at 18th byte in 54 byte array). Rest of the time the function returns a proper result.


I'm using this function to serialize single object

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;
}

I am not serializing any custom classes, only strings and numeric types (firebird api returns them as objects though). I use this to deserialize:

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;
}

and main fnct in client aplication. Sorry for ugly code,

    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;
    }

What is baffling me is that it works in most cases, but inevitably causes to throw an error. Thing that it happens on random makes pinpointing it harder. Please help.


@EDIT This is how I read the input:

    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;
            }
        }
    }

and then serialize with this function

    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;
    }

解决方案

I have found the bug. The code above works fine, but care for encoding in some cases(!), so feel free to use it. The problem laying in another part of a program, where I mistyped and send 4 bytes BUT the client app was told to receive 8, so in most cases it filled it in with zeros, but sometimes it got it from next pack of data.

It was @Marc Gravell and his blog that made me look over and over again to eventually find the source.

这篇关于二进制流“0”不包含一个有效的BinaryHeader。随机出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆