二进制流“NN”不包含有效的 BinaryHeader

发布于 2024-08-15 06:18:19 字数 1156 浏览 7 评论 0原文

我通过套接字传递用户定义的类。 SendObject 代码如下。它在我的本地计算机上运行,​​但是当我发布到 WebServer(然后与我自己的计算机上的应用程序服务器通信)时,它失败了。

    public bool SendObject(Object obj, ref string sErrMsg)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, obj);
            byte[] byArr = ms.ToArray();
            int len = byArr.Length;
            m_socClient.Send(byArr);
            return true;
        }
        catch (Exception e)
        {
            sErrMsg = "SendObject Error: " + e.Message;
            return false;
        }
    }

如果它是我的工具项目中的一个类,而关于 UserData 的另一个类只是不想知道,我可以做得很好。令人沮丧!

哦。我认为这是因为 UserData 类里面有一个 DataSet 。有趣的是,我已经看到了这个工作,但是在 1 个请求之后它变得疯狂,我无法让它再次工作。

有人知道为什么会这样吗?我比较了这些 dll,以确保它们在 WebServer 和我的本地计算机上是相同的,而且它们看起来是这样,因为我在 AssemblyInfo.cs 中打开了版本控制来仔细检查。

可能的原因是序列化和反序列化之间无效的流或对象版本更改。


编辑

好吧,问题似乎出在尺寸上。如果我将其保持在 1024 个字节以下(我在这里猜测),它可以在 Web 服务器上运行,但如果它内部有一个 DataSet,则不会。k 事实上,这太令人费解了,我使用 ds.GetXml 将 DataSet 转换为字符串( ),这也会导致它爆炸。 :( 所以看来,在网络上我的套接字的某些东西是错误的,并且不想读取数据。

I am passing user defined classes over sockets. The SendObject code is below. It works on my local machine, but when I publish to the WebServer which is then communicating with the App Server on my own machine it fails.

    public bool SendObject(Object obj, ref string sErrMsg)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, obj);
            byte[] byArr = ms.ToArray();
            int len = byArr.Length;
            m_socClient.Send(byArr);
            return true;
        }
        catch (Exception e)
        {
            sErrMsg = "SendObject Error: " + e.Message;
            return false;
        }
    }

I can do this fine if it is one class in my tools project and the other class about UserData just doesn't want to know. Frustrating!

Ohh. I think it's because the UserData class has a DataSet inside it. Funnily enough I have seen this work, but then after 1 request it goes loopy and I can't get it to work again.

Anyone know why this might be? I have looked at comparing the dlls to make sure they are the same on the WebServer and on my local machine and they look to be so as I have turned on versioning in the AssemblyInfo.cs to double check.

Possible causes are invalid stream or object version change between serialization and deserialization.


Edit:

Ok it seems that the problem is with size. If I keep it under 1024 byes ( I am guessing here) it works on the web server and doesn't if it has a DataSet inside it.k In fact this is so puzzling I converted the DataSet to a string using ds.GetXml() and this also causes it to blow up. :( So it seems that across the network something with my sockets is wrong and doesn't want to read in the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2024-08-22 06:18:19

这是通过我在这里发布的一个更好的问题解决的:

仅当尝试增长字节数组时,通过套接字发送大型序列化对象才会失败,但在使用大型字节数组时则可以

显然,我有一个从一些玩具示例中提取的静态缓冲区,然后当我开始传递填充的数据集时,它们太大了。我遇到的一些动态缓冲区问题的答案也涵盖了这一点。

This is solved by a better question I have posted here :

Sending large serialized objects over sockets is failing only when trying to grow the byte Array, but ok when using a massive byte array

Obviously I had a static buffer that I had lifted from some toy example, and then when I started passing populated datasets they were too large. The answers to some dynamic buffer problems I was having cover this too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文