Serializable

Dec 31, 2007 at 1:07 AM
If I manually Serialize with this code below all is fine with MyObject.

MyObject objectToSerialize = new MyObject ();
Stream s = File.Open(@"c:\out.txt",FileMode.OpenOrCreate);
BinaryFormatter b = new BinaryFormatter();
try
{
b.Serialize(s, objectToSerialize);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
s.Close();
}
But, If I try to add it into the Cache, I got an error below.

NunitTest.IndexusCacheTest.setGetActionListLate : System.Runtime.Serialization.SerializationException : Type 'System.ComponentModel.ReflectPropertyDescriptor' in Assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Can some one help me to figure why I can Serialize the object but not with Indexus?
Dec 31, 2007 at 1:29 AM
I did some more test.

When I try to use some method to serialize FROM Indexus all is Fine...

MyObject objectToSerialize = new MyObject();
Byte[] byteStream;
try
{
IndexusClient.Formatters.Serialization.FileSerialize(objectToSerialize, @"c:\serie.txt");
byteStream = IndexusClient.Formatters.Serialization.BinarySerialize(objectToSerialize);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

}

BUT I still can't set into the Cache...
Coordinator
Dec 31, 2007 at 3:37 PM
as you mention today in our discussion this can be handled while you're using the attribute: NonSerializable() for this property.
Jan 2, 2008 at 3:21 PM


ronischuetz wrote:
as you mention today in our discussion this can be handled while you're using the attribute: NonSerializable() for this property.


No it doesn't. The NonSerializable() tag work for the code I wrote above (manually serialize work when I add NonSerializable()]. BUT using : Provider.Cache.IndexusDistributionCache.CurrentProvider.Add(key, obj); I get the error that you can see in the first post.

This is weird.
Jan 2, 2008 at 5:03 PM
NunitTest.IndexusCacheTest.setGetActionListLate : System.Runtime.Serialization.SerializationException : Type 'System.ComponentModel.ReflectPropertyDescriptor' in Assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Stack:

at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
at MergeSystem.Indexus.WinServiceCommon.Formatters.Serialization.BinarySerialize(Object obj)
at MergeSystem.Indexus.WinServiceCommon.CacheUtil.Add(String key, Object obj, DateTime expires, String host)
at MergeSystem.Indexus.WinServiceCommon.Provider.Cache.IndexusSharedCacheProvider.Add(String key, Object obj, DateTime expires, String host)
at MergeSystem.Indexus.WinServiceCommon.Provider.Cache.IndexusSharedCacheProvider.Add(String key, Object obj)
at Publipage.IndexusCaching.CacheFacade.set(Object obj, Int32 flag) in C:\p\publipage\publinet\CVSROOT\SOURCECODE\PubliNetSolution\IndexusCaching\CacheFacade.cs:line 64
at Publipage.Caching.CacheFacade.set(Type persistance, Object obj, Int32 flag) in C:\p\publipage\publinet\CVSROOT\SOURCECODE\PubliNetSolution\Caching\CacheFacade.cs:line 87
at NunitTest.IndexusCacheTest.setGetActionListLate() in C:\p\publipage\publinet\CVSROOT\SOURCECODE\PubliNetSolution\NunitTest\IndexusCacheTest.cs:line 140
Coordinator
Jan 2, 2008 at 10:39 PM
Edited Jan 2, 2008 at 10:39 PM
I found some blog entries were this problem is described and a solution is offerd:

Demonstrating BindingList Serialization Bug
Fixing BindingList Deserialization
Custom Data Binding, Part 2

the whole solution and key parts are described in the mention blog entries:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace MyBindingList
{
	[Serializable]
	public class FixedBindingList<T> : BindingList<T>
	{
		[OnDeserialized]
		private void OnDeserialized(StreamingContext context)
		{
			List<T> items = new List<T>(Items);
			int index = 0;
			// call SetItem again on each item to re-establish event hookups
			foreach (T item in items)
			{
				// explicitly call the base version in case SetItem is overridden
				base.SetItem(index++, item);
			}
		}
	}
 
}

after you add this code part to your solution you will be able to work without any problems.

Jan 3, 2008 at 1:00 AM


ronischuetz wrote:
I found some blog entries were this problem is described and a solution is offerd:

Demonstrating BindingList Serialization Bug
Fixing BindingList Deserialization
Custom Data Binding, Part 2

the whole solution and key parts are described in the mention blog entries:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace MyBindingList
{
	[Serializable]
	public class FixedBindingList<T> : BindingList<T>
	{
		[OnDeserialized]
		private void OnDeserialized(StreamingContext context)
		{
			List<T> items = new List<T>(Items);
			int index = 0;
			// call SetItem again on each item to re-establish event hookups
			foreach (T item in items)
			{
				// explicitly call the base version in case SetItem is overridden
				base.SetItem(index++, item);
			}
		}
	}
 
}

after you add this code part to your solution you will be able to work without any problems.




Exact and add the tag field: NonSerialized() over all event that is raised in the serialization (Exemple the Event from IPropertyNotification)

Thank ! You are very fast and bright!
Mar 22, 2010 at 9:41 AM

Can I add the no serializable object into cache by SharedCache?

Because a lot of no serializable objects in my project,

and these objects are in other library,

So I change them to  serializable are very difficulty. ⊙﹏⊙b

Can I change SharedCache code to support no serializable object?

Coordinator
Mar 22, 2010 at 8:01 PM
By default this is not possible. You would have to do a huge set of
adaptions to fullfill this.


Kind regards,
Roni Schuetz

Sent from my iPhone
Mar 23, 2010 at 1:58 AM

Oh~ Thanks Roni Schuetz  ^_^

I hope this feature will be into the roadmap in the future.