Some time ago I asked a question about serializing a collection to a byte array and saving that. It resulting in this blog post.

I modified the Serialize and Deserialize functions to take a generic T type and I created collections which extend from ObservableCollection<t>. I uploaded a sample here.

protected override void OnInitialize()

    if ((PersistentSegments != null) && (PersistentSegments.Length > 0))
        _segmentCollection = Deserialize<Segment>(PersistentSegments) as NotifiableContinuousCollection<Segment>;
        _segmentCollection = new NotifiableContinuousCollection<Segment>();

    if (_segmentCollection != null)
        _segmentCollection.CollectionChanged += OnCollectionChanged;
        _segmentCollection.CollectionItemChanged += OnCollectionItemChanged;

    Session.Events.Persisting += OnPersisting;

private void OnPersisting(object sender, EventArgs e)
    if (HasChanged)
        PersistentSegments = Serialize(_segmentCollection);
        HasChanged = false;

I also have an add function which looks like this:

public void AddSegment(double distance, double value)
    _segmentCollection.Add(new Segment {Distance = distance, Value = value});
    Distance += distance;

Adding the first element is not a problem. When I add the second element I see that the Persisting event handler is called (I would have expected this to happen only when I'm actually calling .Save()) and it results in an exception:

SerializationContext is required. Use SerializationScope to set it.

What am I doing wrong here?

In the project I also included some extension methods: ToCollection() and ToByteArray(). If I use those the serialization works, but when I try to retrieve an item from the database on the second run of the program it throws an exception saying _segmentCollection.Count throw a NullReferenceException. The collection is correctly initialized as far as I can tell.

asked May 05 '11 at 06:04

jensen's gravatar image


edited May 05 '11 at 06:12

Hello jensen,

I'll check the case. Thanks for the sample!

(May 05 '11 at 10:01) Dmitri Maximov Dmitri%20Maximov's gravatar image

I currently worked around it by creating FillFromByteArray functions (instead of ToCollection) in the respective classes by using a ISerializationSurrogate implementation for each respective class that inherits from ObservableCollection.

I also tried by converting my collections to a standard List<t> before serializing but that eventually threw the same context exception.

(May 05 '11 at 11:06) jensen jensen's gravatar image

One Answer:

Hello jensen,

After some investigation I've understood that the problem might be in the following:

  1. NotifiableContinuousCollection.OnCollectionChangedEventHandler method signs to Segment.PropertyChanged event with CollectionItemPropertyChangedHandlerDelegate.

  2. The latter has a reference to AbstractLevel.OnCollectionItemChanged method.

  3. In AbstractLevel.Serialize method binary serializer tries serializing each Segment.PropertyChanged item and the whole graph of references around it and as a result, also makes an attempt to serialize an instance of ConcreteLevel class, which in turn throws the exception.

answered May 10 '11 at 09:29

Dmitri%20Maximov's gravatar image

Dmitri Maximov

edited May 10 '11 at 09:30

That's interesting. I'll look into that.

(May 12 '11 at 09:39) jensen jensen's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview


Once you sign in you will be able to subscribe for any updates here



Asked: May 05 '11 at 06:04

Seen: 2,553 times

Last updated: May 12 '11 at 09:39

powered by OSQA