Csla.Validation.BrokenRule cannot be serialized because it does not have a parameterless constructor.

Csla.Validation.BrokenRule cannot be serialized because it does not have a parameterless constructor.

Old forum URL: forums.lhotka.net/forums/t/1056.aspx


virendra posted on Monday, August 28, 2006

Hi All,

I am serializing my object from xml but i am getting followng error.

{"Csla.Validation.BrokenRule cannot be serialized because it does not have a parameterless constructor."}

My Business Object derives from CSLA.BusinessBase.

I just want to create my object from XML. The xml does not contan any kind of BorkenRule collection.

Is there any way by which I can ignore this property during serialization.

Regards,
Virendra

hurcane replied on Tuesday, August 29, 2006

virendra:
Hi All,
I just want to create my object from XML. The xml does not contan any kind of BrokenRule collection.


As Rocky discusses on page 211, the XMLSerializer object has lots of limitations that prevent it from being useful for mobile business objects. However, you can implement custom serialization of your business objects. Custom serialization allows you to specify exactly which fields you want included.This is a lot of work, and it has to be coded by hand for each business object, but it's probably the best way for you to get your object to XML. You might consider implementing this in a "ToXML" method.

Rajat replied on Monday, November 20, 2006

Hi,

I am relatively new the CSLA framework. I got the same problem as yours in the inner exception.

In the outer exception, I got something like ..

"There was error reflecting type ListAbc."

This class is derived from BusinessListBase<>. In the innerexception, I got the following error...

"Can not serialize member CSLA.Core.BusinessBase.BrokenRulesCollections of type CSLA.Validation.BrokenRulesCollection can not be serialized." Now on checking the innerexception property of this, I got the same error message as yours "CSLA.Validation.BrokenRule can not be serialized because it does not have a parameterless constructor".

For now, I have changed in the original code of CSLA framework without knowing the repurcutions. I have included a parameterless internal overloaded constructor (besides the one which is already there with few parameters).

When I referenced the new dll, and tried to serialize using the XMLSerializer, I was able to serialize the object. So I have the answer of the problem but still need to know the cause of this issue.

Regards,

Rajat.

 

ajj3085 replied on Monday, November 20, 2006

Well the cause is just what the exception says; there's no parameterless constructor, so the XmlSerializer can't deserialize.

There's other limitations as well.  All properties must have a public setter, or the fields must be publically settable.  Neither of those are great for business objects.

RockfordLhotka replied on Tuesday, November 21, 2006

DO NOT implement ISerializable for this purpose!!

ISerializable allows you to control how the BinaryFormatter serializes your object - it DOES NOT affect how the XmlSerializer works. All you'd do by implementing ISerializable is mess up cloning and the data portal.

There's a different interface you need to implement to control the XmlSerializer (I don't remember the interface name). It didn't used to be documented, as it was for internal use in the DataSet only, but I think in .NET 2.0 Microsoft documented the interface, so it is probably OK to use.

I don't know if that interface is honored by WCF though, so you might want to check on that ahead of time. Somehow I doubt they support it, because WCF has a new mechanism for serialization. However, they do support <Serializable()> and ISerializable, so it is hard to say for sure.

My recommendation, as always, is that if you want to expose your objects as XML, you should define a formal DTO (data transfer object) and write code to put your data into/out of that DTO - then let the XmlSerializer (or WCF) serialize the DTO into/out of XML.

That approach gives you much better control over the process, and decouples your object from the XML contract you are defining - making your code more maintainable in the long term.

ajj3085 replied on Tuesday, November 21, 2006

Sorry, I removed my awful suggestion.

The ISerializable interface documentation doesn't seem to indicate that XmlSerializer ignores it completely, but I guess that makes sense since it doesn't seem to implement IFormatter.


Copyright (c) Marimer LLC