views:

351

answers:

3

I have a fairly complex object model that I'm trying to serialize using WCF. I'm running into a problem where I can create a new instance on the server and return it to the client, then trying to pass that same object back or even serialize it using the DataContractSerializer throws an exception.

Test method Server.Service.Test.SerializationTest.TestFilingSerialization threw exception: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type MyNameSpace.MyObject. No set method for property '' in type ''

It's not giving me any info on a setter I'm supposedly missing, even though I've tried looking through the inheritance hierarchy for missing setters, I can't seem to get passed this. MyObject is a type of property on the object I'm trying to serialize, here's the funny part. In the setter for that property, I have code that looks like this:

set
{
    _backingField = value;
    _backingField.IsDirtyManager.SetIsDirty();
    NotifyPropertyChanged("MyProperty");
    if (!ContainsType(_backingField.GetType()))
    {
        Aggregates.Add(_backingField);
    }
}

If I comment out the Aggregates.Add it goes onto the next property and throws the same exception there, the kicker is, it get's through other properties with the same code, before it hits this one and dies, so I don't know what the problem could be.

Anyone have experience where the real error is, because the Property name and Type are not filled out in the exception so it seems like the error has to be something else. If I just create a new instance on the client I can deserialize and serialize no problems, so there's gotta be something I'm missing here. Any ideas on what to look for?

EDIT

I am literally doing only this:

  1. Create instance
  2. Serialize
  3. De-serialize
  4. Re-serialize
    1. Die here
+1  A: 

I am assuming that the "Aggregates" is a list in your class.

If your class constructor does not create an Aggregates list, then it is null when you try to add items to it.

This does not match the error you are getting, but worth checking.

EDIT

In Visual Studio go into "Tools" -> "Options" -> "Debugging" -> "General" uncheck the option "Enable Just My Code". Then run it again, you may get a better error message.

Shiraz Bhaiji
It's not null, I've even tried adding an OnDeserialized method, which does get called that checks for these things
Nick
To remove any extra variables I have a unit test that creates a new instances and uses the DataContractSerializer directly to serialize, deserialize and re-serialize. The third step is where it dies. I'm sure there's something happening in this object or a setter that's messing it up. But I need to find out where.
Nick
I did get a different error, see edited question.
Nick
False alarm, it looks like I just made a mistake in reserializing to a file twice so it appended on...still at the original error
Nick
A: 

You might see this if your setter is protected or privet. If it's not marked public the serializer will not be able to set it. How do you have your Aggregates collection exposed?

Aaron Fischer
Aggregates is a public List<T>. This is happening on the Serialization, I've traced it to the point where it returns the object from the property and then it pukes. If I try to serialize just that property, it works fine.
Nick
+1  A: 

Ok, figured it out. The last developer had set the Order property on the DataMember attribute. It was the property with the order before the property in the error message that was causing the problems. There were a couple read-only properties that I missed in that class.

Nick
Could you add this to the description and indicate that it has been resolved? That would be helpful for people like me coming to this post looking for the answer. Also, I posted here (http://endlessobsession.com/2010/07/12/datacontractserializer-serialization-error/) about the same issue and describe a slightly different scenario.
Bryan Matthews