Viewstate Serialization issue

Topics: Developer Forum, User Forum
Oct 2, 2007 at 7:12 PM
I have an object that inherits from NotifyValidatableBase. Many of the properties of this object call NotifyAndValidate. In the Widows world where I don't have to worry about state this works great, however, I have a web app in which I am having issues with objects being deserialized from viewstate. In order to move forward with the project I moved to session state. I would like to still use viewstate if at all possible, is there any thing I need to do prior to deserializing the object?

The issue, that I've observed is that propertyValidationManager of validatablebase is no longer instantiated and is null when the object is deserialized from viewstate.

Thanks in advance....
Oct 2, 2007 at 7:53 PM
Murphy's Law, post an issue and find a solution minutes after posting......

To use viewstate as a means of storing an object I found that I need to Instantiate PropertyValidationManager and pass the object which inherits from NotifyValidatableBase. Then obtain the target.

_newobject = CType(ViewState("Claimant"), MyObject)
Dim obj As New ValidationFramework.PropertyValidationManager(_newobject)
_newobject= obj.Target
This worked great for me and was able use viewstate instead of session state. If there is a more elegant way/correct way of doing this please enlighten me.

Oct 5, 2007 at 12:40 AM
I will have a look into this one.
It would make it a lot easier if you could write a Unit test for me that illustrates the problem.
Oct 15, 2007 at 9:25 PM
Thank you for your reply. I was wrong in stating my last post. It did not resolve the issue. I had another project in which I needed to serialize the object to a file and when I tried to deserialize and attempt to validate or manipulate any property in which I call NotifyAndValidate("PropertyName") I get a null exception. this is identical to the serialize to viewstate mentioned above. Currently I am referencing runtime version v2.0.50727. Lastly, I am ashamed to say that I've never created a unit test much less analyze the results. I will research this and post my results. Again thank you.
Oct 16, 2007 at 1:45 PM
I briefly researched what I needed to do to configure a project to utilize NUnit. Here is my results after running the test

at ValidationFramework.DataErrorInfoValidatableBase.ValidateProperty(String propertyName) in C:\Documents and Settings\SomeUser\Desktop\ValidationFramework-23258\Validation\ValidationFramework\BusinessBaseClasses\DataErrorInfoValidatableBase.cs:line 108
at ValidationFramework.NotifyValidatableBase.NotifyAndValidate(String propertyName) in C:\Documents and Settings\SomeUser\Desktop\ValidationFramework-23258\Validation\ValidationFramework\BusinessBaseClasses\NotifyValidatableBase.cs:line 77
at TestValidation.ValidationTest.TestProperty() in C:\Projects\VB2005\Sandbox\ValidationTest.vb:line 25

Below is a simple class that I used to serialize and deserialize which produced the error:

Imports ValidationFramework
Imports NUnit.Framework

<Serializable(), NUnit.Framework.TestFixture()> _
Public Class ValidationTest
Inherits NotifyValidatableBase

Private _firstname As String = String.Empty

<ValidationFramework.RequiredStringRule()> _
Public Property FirstName() As String
Return _firstname
End Get
Set(ByVal value As String)
_firstname = value
End Set
End Property

<Test()> _
Public Sub TestProperty()
Dim serialized As String = SerializeObjectToString(Me)
Dim obj As ValidationTest = CType(DeserializeObject(serialized), ValidationTest)
'Throws an exception here
obj.FirstName = "Jane Doe"

End Sub
<SetUp()> Public Sub Init()
_firstname = "Jon Doe"
End Sub

''' <summary>
''' Serializes object to string
''' </summary>
''' <param name="obj"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function SerializeObjectToString(ByVal obj As Object) As String
Dim output As New System.Web.UI.LosFormatter
Dim writer As New IO.StringWriter
output.Serialize(writer, obj)
Return writer.ToString()

End Function

Public Shared Function DeserializeObject(ByVal value As String) As Object
Dim input As New System.Web.UI.LosFormatter

Return input.Deserialize(value)
End Function

End Class

Oct 17, 2007 at 2:54 PM
I think I managed to bypass the error when deserializing the object.
I modified the ValidationBase.cs file so that when the readonly property PropertyValidationManager is called that it checks to see if local variable propertyValidationManager is null, if so instantiate it
It's a hack but it works for the time being.
public PropertyValidationManager PropertyValidationManager
if (propertyValidationManager == null)

propertyValidationManager = new PropertyValidationManager(this, null, null );
return propertyValidationManager;