Tuesday, May 29, 2012

Sterling Serialization issue - Object of type 'System.Int32' cannot be converted to type 'System.DateTime'

sterling forksI created a new data model class to store in a Sterling NoSQL database. On the first and all subsequent attempts to save or retrieve data of this type, I received the error message at the bottom of this post. A number of other people reported similar issues. Apparently it is some type of serialization/deserialization issue. I didn’t find a suggested resolution, but on a whim, moved the AssessmentDate DateTime property from the top of the list of properties in the class to the bottom and that seemed to resolve the issue.

class definition

Object of type 'System.Int32' cannot be converted to type 'System.DateTime'.

   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Wintellect.Sterling.Serialization.PropertyOrField.<get_Setter>b__0(Object obj, Object prop)
   at Wintellect.Sterling.Serialization.SerializationHelper.<>c__DisplayClassd.<_CacheProperties>b__5(Object parent, Object property)
   at Wintellect.Sterling.Serialization.SerializationHelper.Load(Type type, Object key, BinaryReader br, CycleCache cache)
   at Wintellect.Sterling.Database.BaseDatabaseInstance.Load(Type type, Object key, CycleCache cache)
   at Wintellect.Sterling.Database.BaseDatabaseInstance.Load(Type type, Object key)
   at Wintellect.Sterling.Database.BaseDatabaseInstance.Load[T,TKey](TKey key)
   at Wintellect.Sterling.Keys.TableKey`2.<.ctor>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at LeanAssessmentNav.Views.Register.<BindLists>b__6(TableKey`2 a)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at LeanAssessmentNav.Views.Register.BindLists()
   at LeanAssessmentNav.Views.Register..ctor()

 

photo credit: Le Petit Poulailler / CC BY 2.0

Why isn’t my Sterling database persisting between application loads?

Sterling repository (silver bowl)I’m using Sterling, a NoSQL database for .Net, Silverlight, and Windows Phone, to persist some data in a Silverlight 4 application. The intent is to persist it using Isolated Storage. It was obvious that the data was temporarily persisting because the stored data was available on multiple different views in the application. The trouble showed up when I stopped debugging and later started debugging again, the application re-launched and none of the previously persisted data was available.

Initially I was thinking that using the Visual Studio ASP.Net Development server was not maintaining Isolated Storage between application launches. It was far more simple than that. By default, Sterling uses an in-memory driver. If you want to persist using Isolated storage, you must pass an instance of the IsolatatedStorageDriver as an argument when Registering the Database. Note the difference in the samples below.

In Memory

Database = _engine.SterlingDatabase.RegisterDatabase<YourSterlingDatabase>();

Isolated Storage

Database = _engine.SterlingDatabase.RegisterDatabase<YourSterlingDatabase>(new Wintellect.Sterling.IsolatedStorage.IsolatedStorageDriver());

 

photo credit: Neil Noland / CC BY-SA 2.0