views:

79

answers:

1

I've created a WCF service in which I would like it to maintain state between calls from the client. I figured the easiest way to do this was to add this attribute to the service:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

since this is supposed to keep a separate service alive for each client over the life of the client proxy (or timeout in the extreme case). I also added a test function that tracks a list of user inputs, and spits out a concatenated string with all the inputs over the life of the service.

When I run this in the test client generated by visual studio, I find that the list I was using to hold past data is reset with each call. Is there something else I need to do to maintain state per session?

+1  A: 

Ok figured it out. I was using a BasicHttpBinding, which doesn't support per session instancing. I switched it to a wsHttpBinding, and presto, everything works.

tbischel
It might work, but it's generally thought to be a "hack" - whenever possible try to remain stateless and/or store state on the server backend. Do not rely on sessions - just too many things that can go wrong and will wreck havoc on your system...
marc_s