views:

66

answers:

2

I am changing my ASP.NET app to use a web farm. To do this, I need to change the session state from in-proc to a State Server. To do this, it is my understanding that the classes that are used must be marked as serializable. How can you tell if that is possible with a class? Will you get an error at compile time if it is not possible?

+1  A: 

In answer to your first question about how to tell whether or not a class is serializable, see the following discussion. http://stackoverflow.com/questions/81674/how-to-check-if-an-object-is-serializable-in-c

Unfortunately, .net has surprised me with bizarre behavior when I try to serialize objects that I think should be serializable.

For example, in my WCF projects, I can serialize and transmit DataSet objects. However, if I try to serialize and transmit DataTable objects, I end up with a blank DataTable. It took me a while to track that one down.

Therefore, I would suggest that you do at least some rudimentary testing of what happens when you try to serialize custom classes.

Rice Flour Cookies
This isn't an answer, it's a comment.
ChrisF
+1  A: 

You won't get a compile-time exception, since compile-time doesn't really know whether the objects will need to be serialized. You'll get a SerializationException when IIS attempts to serialize your objects.

You can write a short snippet that attempts to serialize and de-serialize the objects in question... use a BinaryFormatter to do the serialization, and a FileStream to write to.

The ObjectBrowser will tell you if an existing class implements ISerializable. If you're looking at your own objects to serialize, keep in mind that classes must really be designed for serialization if they are intended to be serialized, otherwise there are gotchas. For example, from the MSDN help:

The order in which objects are deserialized cannot be guaranteed. For example, if one type references a type that has not been deserialized yet, an exception will occur.

I recommend reading up in ISerializable... here's a link: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx


EDIT: Here is a simple example of serialization and deserialization... just replace the Exception I'm serializing with your own objects:

BinaryFormatter formatter = new BinaryFormatter();

Exception serializedException = new Exception("Testing serialization");
Exception deserializedException;

using (FileStream fileStream = new FileStream(@"C:\SerializationTest.txt", FileMode.CreateNew)) {
    formatter.Serialize(fileStream, serializedException);
}

using (FileStream readStream = new FileStream(@"C:\SerializationTest.txt", FileMode.Open)) {
    deserializedException = formatter.Deserialize(readStream) as Exception;
}

if (deserializedException != null) {
    throw deserializedException;
}
James B
Thank you for the information. Is there anyway to see a quick code snippit that shows what you are referring to with the BinaryFormatter and FileStream?
Added example code... did that help?
James B