Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:17:54+00:00 2026-06-07T18:17:54+00:00

Here is a structure I serialize in my project: [Serializable] class A : List<B>

  • 0

Here is a structure I serialize in my project:

[Serializable]
class A : List<B> //root object being serialized

[Serializable]
class B
  + [A few serializable fields]
  + C customList

[Serializable]
class C : List<D>

[Serializable]
class D
  + [several serializable fields]
  |
  + [NonSerialized] nonserializable3rdPartyClass data
  + string xmlOf3rdPartyData
  |
  + [OnSerializing]
  + private void OnSerializing(StreamingContext context)
  |
  + [OnSerialized]
  + private void OnSerialized(StreamingContext context)
  |
  + [OnDeserialized]
  + private void OnDeserialized(StreamingContext context)

The nonserializable3rdPartyClass, although not marked as [Serializable], provides .ToXml and .FromXml methods which I use in my .OnSerializing and .OnDeserialized methods, respectively, to store and retrieve the XML string in xmlof3rdPartyData.

I’ve recently come across an issue where, under certain unknown circumstances (I have so far only been able to reproduce the issue using a serialized data file from a client, who first reported the issue), my .OnSerializing and .OnSerialized methods are only being called 57/160 times (where 160 is the total number of D objects in the structure) when using a BinaryFormatter to serialize to a file, leaving me with 103 D objects with xmlOf3rdPartyData set to null. When cloning the structure using the method described here (which is basically the same as what I use to serialize to a file), I see the same results for .OnSerializing/.OnSerialized, but my .OnDeserialized method is called the full 160 times.

This bit of code has been in use for months without issue (at least, as far as I know), and I’m still trying to determine why this is happening now and not earlier. I’m not seeing any first chance exceptions while debugging, and my breakpoints at the start of the methods are simply not being hit more than 57 times. Any ideas on why this would occur/how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:17:57+00:00Added an answer on June 7, 2026 at 6:17 pm

    After a few days of digging, I discovered that the problem was both my fault and a possible bug in the .NET Framework.

    The .NET half of the problem

    While poking around in the stacktrace for my OnSerializing method, I came across the RegisterObject method in System.Runtime.Serialization.SerializationObjectManager, which determines whether to call any OnSerializing methods in the object being serialized. It determines this in two ways (this is based off decompiled code from .NET Reflector):

    1. Does the class have any OnSerializing methods to call
    2. Is this an previously unseen object (within this call to BinaryFormatter.Serialize)

    Number 2 is the problem child. It tracks objects that have already been seen by storing them as an object/bool pair in a Hashtable (which uses GetHashCode, of course). If either of these is false, the object’s OnSerializing methods are not called. This apparently works fine in the vast majority of situations (otherwise Microsoft would have fixed it at some point, right?), except for the one I seem to have stumbled upon.

    My half of the problem

    Simply enough, I forgot to include the non-serializable field in my GetHashCode for my D class, so I was getting collisions. Stupid mistake, I know, don’t know how I missed it.

    But wait…

    …wouldn’t that mean that it’s not .NET’s fault at all, just my own? No, and here’s why. I expect OnSerializing and OnSerialized methods to be called 100% of the time no matter what. No where in the docs does it say otherwise. When that doesn’t happen my objects aren’t serialized correctly, and I end up spending way more time than I’d like trying to solve mysteries. Even if two identical objects are being purposefully serialized, they apparently don’t end up pointing to the same binary data/location in the Stream, so they don’t deserialize the same. I’d consider this a bug, not a feature.

    I’ve written up a test-case that demonstrates all this. If I’m doing anything blatantly wrong I’d appreciate feedback saying so, otherwise I’ll probably post this on the MSDN forums or as a Connect bug. And before anyone suggests, I’ve planned on switching away from BinaryFormatter for some time now for all the various reasons posted elsewhere on SO, I’ve just have more important things to deal with.

    Edit: Apparently this bug was filed over a year and a half ago.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have a file name call A.java. Here its structure: public class
Here is my project structure: /app --/lib ----/porter.rb --/spec ----/porter_spec.rb In file porter_spec.rb i
Here is the basic structure of my User class: class User { private $_userId
Here is the structure of the data my object will need to expose (the
Here is a structure used in a program: struct basic_block { void * aux;
okey so here is my structure. one.php two.php three.php one.php includes both two.php and
I have a document structure {'text': 'here is text', 'count' : 13, 'somefield': value}
I have a complex form in Ruby on Rails 2.3.5, here's the structure for
Here is the simplified table structure of PRICES : SKU PriceType FromDate ToDate Price
Here's the XAML structure. You'll see below that I'm subscribing to the Loaded event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.