对象类型'ImagePacket'没有程序集ID。序列化异常捕获C# [英] No assembly ID for object type 'ImagePacket'.De Serialization Exception Caught C#
问题描述
我在 UDP套接字上发送序列化大图像对象。当我在内存流中写入所有接收到的字节并传递内存流对象进行反序列化时,它会抛出异常没有对象的程序集ID输入'ImagePacket'。
I am Sending Serialized large Image Object over UDP Socket.When I write all received bytes in Memory stream and pass the memory stream object for deserialization it throws an exception No assembly ID for object type 'ImagePacket'.
接收方结束代码:
ImageStream = new MemoryStream();
while (AccumulatingBytes <= TotalSizeOfComplexObject)
{
byte[] Recievedbytes = UdpListener.Receive(ref RemoteEndPoint);
ImageStream.Write(Recievedbytes, 0, Recievedbytes.Length);
AccumulatingBytes += Recievedbytes.Length;
}
ImageStream.Position = 0;
imagecontainer = (ImageContainer)bformater.Deserialize(ImageStream);//Here the Code Segment Breaks and Exception thrown
推荐答案
我怀疑这里的问题很简单:你使用UDP就像TCP一样。 UDP是基于数据包的,但是:不保证数据包将按顺序到达,并且b:不保证数据包不会被丢弃或重复。
I suspect the problem here is simply: you are using UDP like it is TCP. UDP is packet based, but a: doesn't guarantee that the packets will arrive in order, and b: doesn't guarantee that packets won't be dropped or duplicated.
我完全希望你有一些故障。如果您要发送多条消息,也可能会丢弃一些消息,并且您在下一条消息中包含了一些消息。
I fully expect you have some out of order. If you are sending multiple messages, it is also possible some were dropped, and you've included a few from the next message.
以您的代码的方式使用网络想要使用它:使用TCP。否则,理解无序,丢弃和重复数据包的责任完全由您自己承担。例如,这可以通过向分组添加序列号,并跟踪已接收的内容 - 根据需要重新排序它们,丢弃重复项,并重新请求任何已经死亡的路径。基本上,重写TCP添加的所有内容!除非你有一个非常具体的场景,否则TCP堆栈(具有NIC和OS级别支持)很可能会比你更好地完成这项工作。
To use the network the way your code wants to use it: use TCP. Otherwise, the responsibility for making sense of out-of-order, dropped and duplicated packets is entirely yours. This could be, for example, by adding a sequence number to the packet, and keeping track of what has been received - re-ordering them as necessary, dropping duplicates, and re-requesting any that died en-route. Basically, re-writing everything that TCP adds! Unless you have a very specific scenario, there's a good chance that the TCP stack (with NIC and OS level support) will do a better job of this than you will.
这篇关于对象类型'ImagePacket'没有程序集ID。序列化异常捕获C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!