views:

1272

answers:

4

I am trying to store some objects in the session (which is using a StateServer), but I am getting the error "System.Web.HttpException: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode"

I know what the error message means, but I can't work out why. All of the classes I am using are marked as Serializable, and I am able to Serialize and Deserialize the object to and from XML using:

System.IO.StringReader stringReader = new System.IO.StringReader(xml);
System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader(stringReader);
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Parts));
Parts obj = ((Parts)(xmlSerializer.Deserialize(xmlTextReader)));

This works, and will Serialize as well using:

System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
xmlSerializer.Serialize(memoryStream, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamReader streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();

But the error is thrown when trying to store it in the Session.

Does anyone have any ideas what may be causing this behaviour?

EDIT:

I have just discovered that this line is causing the error (having removed everything and re-included it)

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("RecordReference", typeof(RecordReference), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("PartContainer", typeof(PartContainer), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public object Item
{
    get
    {
        return this.itemField;
    }
    set
    {
        this.itemField = value;
    }
}

If I set this "Item" property to "new RecordReference()", then the error occurs. If it is null, it's fine.

So now, the question is, why can't the StateServer cope with this? It serializes fine when serializing to XML...

EDIT...

Type 'System.Xml.XmlElement' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

.....Are we saying that the Xml objects in C# aren't serializable?! Does anyone else think this verges on the insane?

A: 

The biggest thing is to ensure that your web.config has your session state mode set as 'InProc'. Based on the error message you are receiving, simply changing that should fix it. If you still encounter issues, make sure that any events are marked as non-serializable.

PortageMonkey
I'd assume that choosing StateServer was deliberate, since InProc is the default.
stevemegson
It was...changing it isn't an option I'm afraid
Paul
+3  A: 

In the stack trace you should see a SerializationException that will mention which class it's unable to serialize...

[SerializationException: Type 'SomethingOrOther' in Assembly 'SomethingElse' ...

Note that the state server uses binary serialization not XML serialization.

stevemegson
The type is:System.Runtime.Serialization.SerializationException: Type 'System.Xml.XmlNode' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
Paul
I'm aware StateServer uses binary serialization - are we saying that binary serialization can't serialize a property of type "object"?
Paul
I don't think it cares about the type of the property, just the object that happens to be assigned to it (so you're fine when it's left null). I think we're saying that binary serialization can't handle an XmlNode.
stevemegson
+1  A: 

So I have found the answer to the question, but I'm not happy about it.

Basically, some of the classes I'm using contain XMLElements and XMLNodes (they're automatically generated using svcutil). For whatever reason, but it thinks it needs them.

Neither of these XML classes are serializable!! Am I the only one who finds this to be a complete failing of these objects? So to get this set of classes into the session, I've got to serialize them down to a string, and then store that in the session, which is in turn serializing it. So I'm serializing it in order for it to be serialized.....!?

Not sure I'm happy with that, but that was the cause of my problems.

Paul
A: 

As stevemegson said, find out which class threw the SerializationException.

[SerializationException: Type 'SomethingOrOther' in Assembly 'SomethingElse' ...

Find that "SomethingOrOther" class in your code and make sure it is serializable.

Making a class serializable in C#:

   [Serializable]_
      public class PayeeData
      {
         ...
      }

Making a class serializable in VB.NET:

   <Serializable()> _
      Public Class PayeeData
         ...
      End Class

For your specific query about XML serialization, see this MSDN article:

XML Serialization in the .NET Framework

andyuk