Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • Just because Exceptions are not nice, when working with external code or libraries (like that BinaryFormatter), you cannot always avoid them nor does it always make sense to avoid them in the first place. But in the end im on your side with this. However it would have been nice to see your intentions before you complain on that answer or downvote it. Commented Aug 29, 2018 at 14:15
  • The first thing binary formatter does, is to read a header record. In that process, it reads 17 bytes from the stream, and when the resulting data has a length of less than 17 bytes, it throws an end of stream exception. So, as soon as you ask the BinaryFormatter you will have an exception. A "proper" solution, and not uncommon when you design any protocolls, would be to send the number of items in the stream before the first item, and not reading more than that. Commented Aug 29, 2018 at 14:42
  • Found a proper solution. No Try. No Catch. See my (updated) old answer. Commented Aug 29, 2018 at 14:51
  • Just a FYI BinaryFormatter is not recommended for use for inter machine communication or for persisted data. It is extremely intolerant of assembly version changes, if one side has updates the other does not they won't be able to talk to each other. Commented Aug 29, 2018 at 21:11
  • @ScottChamberlain: What do you suggest to use? protobuf-net? Commented Aug 29, 2018 at 21:27