unexpected behaviour of object stored in web service Session
I'm using Session variables inside a web service to maintain state between successive method calls by an external application called QBWC. I set this up by decorating my web service methods with this attribute:
[WebMethod(EnableSession = true)]
开发者_Go百科
I'm using the Session variable to store an instance of a custom object called QueueManager. The QueueManager has a property called ChangeQueue which looks like this:
[Serializable]
public class QueueManager
{
...
public Queue<QBChange> ChangeQueue { get; set; }
...
where QBChange is a custom business object belonging to my web service.
Now, every time I get a call to a method in my web service, I use this code to retrieve my QueueManager object and access my queue:
QueueManager qm = (QueueManager)Session[ticket];
then I remove an object from the queue, using
qm.dequeue()
and then I save the modified query manager object (modified because it contains one less object in the queue) back to the Session variable, like so:
Session[ticket] = qm;
ready for the next web service method call using the same ticket.
Now here's the thing: if I comment out this last line
//Session[ticket] = qm;
, then the web service behaves exactly the same way, reducing the size of the queue between method calls. Now why is that?
The web service seems to be updating a class contained in serialized form in a Session variable without being asked to. Why would it do that? When I deserialize my Queuemanager object, does the qm variable hold a reference to the serialized object inside the Session[ticket] variable?? This seems very unlikely.
All reference types (such as classes) are stored in memory as references and Session State is also (normally) stored in memory, but can also be stored to a file or a database. When storing reference types in Session, you should almost always ensure they are Serializable as well.
QueueManager
is a reference type, so all the Session[ticket]
is doing is holding a reference to it in memory. You do not need to re-assign to it as the Session
variable itself IS the item that you're modifying.
This is just a simplified version of what you're doing:
Session["Foo"] = new Bar();
Bar rar = (Bar)Session["Foo"];
rar.Count = 1;
if (((Bar)Session["Foo"]).Count == 1)
{
// Great success!
}
精彩评论