Serialization of ValidationErrors

Topics: Validation Application Block
Oct 2, 2007 at 3:22 PM
It says in the manual that validation supports multiple layers but I have a problem.

I am passing structures through a web service and want them validated on the server. The validation errors are placed back in the response and returned to the client.

This fails to work with two errors:

1) The ValidationError class has no default (empty) constructor
2) The ValidationErrrors does not implement IEnumerable Add(object)

Is this by design?

thanks

Cliff
Oct 2, 2007 at 4:12 PM


charker wrote:
It says in the manual that validation supports multiple layers but I have a problem.

I am passing structures through a web service and want them validated on the server. The validation errors are placed back in the response and returned to the client.

This fails to work with two errors:

1) The ValidationError class has no default (empty) constructor
2) The ValidationErrrors does not implement IEnumerable Add(object)

Is this by design?

thanks

Cliff



Why does ValidationErrors have a

List<ValidationErrror> Errors

yet a ValidationError has an

IEnumerable<ValidationError> NestedValidationErrors

because I'm getting IEnumerable can't be serialised, it's an interface. Why is this not a List<>?

It seems from the code that ValidationError should be serializable, it is marked with Serializable but how would it ever work? Its properties are also GET-only, so even if they get transferred they get rehydrated empty.

A bit confused!!!
Nov 17, 2007 at 2:53 AM
I would REALLY like to know if there is a way to serialize ValidationResults.... Being able to throw these results over the wire would save me a lot of headaches... for now the only thing I can think of is creating a custom object that takes what I need out of ValidationResults and then recreates ValidationResults back on the other side.
Nov 17, 2007 at 10:30 PM
I looked into the code and made some changes so that I can serialize over the wire. This is not the ideal thing to do but it seems to work (with some pitfalls).

In order to serialize ValidationResults I needed to add a "Add(Object obj)" method

As well, I needed to add an empty constructor for ValidationResult and add "set" accessors for all the properties (this is how WebService's are able to rebuild an object).

I haven't really TESTED this but it seems to be working... It kind of breaks the visibility the original code was trying to follow but at least it now serializes.

One pitfall to watch out for is that if there is an object attached to the Target property, the serialization will probably fail as it does not know what type to expect.

I think it is probably a better idea to create a custom object for going over the wire and just create a couple of factory methods to covert back and forth.
Dec 8, 2009 at 6:15 PM

We are running into the same issue and really need some help;  we want to pass back ValidationResults from our WCF services but are gettig exceptions related to serialization.

Is there a fix that can be made to EL VAB to allow serialization to occur correctly?  Also, if not, what did you do to resolve this.

Thanks in advance for any help you can provide.

Tracy

Dec 8, 2009 at 6:28 PM

As the previous person said, edit the source code...

In order to serialize ValidationResults I needed to add a "Add(Object obj)" method

As well, I needed to add an empty constructor for ValidationResult and add "set" accessors for all the properties (this is how WebService's are able to rebuild an object).

 

Dec 8, 2009 at 6:38 PM

I had a developer do this and he still got the exceptions.  (He's a senior guy so wonder what could be missing).  Were you using WCF or ASMX services?

Any chance you could post your version of ValidationResults and ValidationResult?

Thanks tons,

Tracy

Dec 8, 2009 at 8:15 PM

We used ASMX and basically did what is suggested here. It's a while ago and I'm not sure I can lay my hands on the code or whether we left it in in the end.

WCF is far more fussy about "KnownTypes" for the entire class/object tree, you may have to decorate all your classes with a list of the KnownTypes.

What error are you getting?  if it is "don't know how to serialize xyz" then add xyz as a KnownType