WCF and ValidatableBase

Nov 5, 2008 at 5:51 PM
I have run into a problem with ValidatableBase and WCF in version 1.12. 

In version 1.9 the PropertyValidationManager is initialized in the Property if it is null.

    protected PropertyValidationManager PropertyValidationManager
    {
      get
      {
        if (propertyValidationManager == null)
        {
          //Lazy initialize so that PropertyValidationManager only instantiated when needed
          propertyValidationManager = new PropertyValidationManager(this, ruleSet);
        }

        return propertyValidationManager;
      }
    }

Version 1.12 does not perform this way; propertyValidationManager is only instantiated in the constructor.  The problem is that when WCF derserializes the object it does so without calling a constructor.  This leaves propertyValidationManager as null and causes a NullReferenceException upon calling IsValid.

I realize that I can create my own base class and will do so but I thought I should point this issue out.

The following post suggests doing any necessary initialization in the OnDeserializing callback to support WCF.
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/0005537c-abd8-4d08-91ba-a483d6751a9e/

Perhaps adding a method similar to the one below to initialize propertyValidationManager would solve the problem.

    [OnDeserializing]
    public void OnDeserializing(StreamingContext ctx)
    {
        if (propertyValidationManager == null)
        {
          //Lazy initialize so that PropertyValidationManager only instantiated when needed
          propertyValidationManager = new PropertyValidationManager(this, ruleSet);
        }

    }

 



Coordinator
Nov 11, 2008 at 9:26 AM
Edited Nov 11, 2008 at 9:26 AM
Good catch.

Feel like creating a patch with a unit test and the fix?  :)

If not i will get to it in a couple of days.
Nov 12, 2008 at 11:16 AM
Yes, I can post a patch.  I have already modified the source for my own use.  I will try to do a unit test and post later today.
Nov 12, 2008 at 2:10 PM

See patch id 2076.

Note that ruleset will still not be available through WCF because it is not a property and the constructors will not be called.  I am not currently using rulesets so I haven't looked at fixing this.  I am guessing that we would just need to expose ruleset as a property to allow it being set during deserialization.