Are WCF services stateless by default?
Asked Answered
G

2

9

I've got a simple WCF service that lets clients/consumer applications log in by providing a username and password. If both the username and password are correct, the WCF service provides the client with a GUID. The GUID and the username are then stored as a key/value pair within the WCF service. From here onwards, the client sends their GUID with every request as a means of identification.

Since I'm storing the key/value pair in a Dictionary/Hashmap, this approach would only work if the WCF service is stateful. Question is, are they stateful by default or is there something I have to do to make them behave in that manner?

Gambell answered 18/5, 2011 at 20:22 Comment(3)
WCF services should be stateless - saves you from a lot of thorny issues and headaches. The accepted "Best Practice" is to use "per-call" methods - each call is a totally new, separate operation, fully independent of any other calls before that.Coverdale
@Coverdale : The services I've got are indeed stateless, but I'm just not sure on how to store the key/value pair for identifying clients using the service. Should I be storing these temporary values in a database instead of a Dictionary/Hashmap?Gambell
I would just pass the username/password into each and every call. If that's too hard to validate each time: pass back some identifier from the first call and store that identifier in a table "current valid users" or something like that. On subsequent calls, just pass in that "identifier" and check it against the "current valid users" tableCoverdale
S
9

Per-call services are the Windows Communication Foundation default instantiation mode. So, by default, WCF services do not maintain state. As Marc stated, there are potential problems with saving state in WCF services. I strongly suggest heeding his advice.

This article describes various ways to handle instance management in WCF, including how to maintain state like you're asking for.

Chapter 4 of Juval Lowy's excellent Programming WCF Services (link) goes into much greater detail.

Salas answered 18/5, 2011 at 20:33 Comment(4)
I believe the default is PerSession for channels that support sessions?Darnell
I grabbed that information from Juval Lowy's article here: msdn.microsoft.com/en-us/magazine/cc163590.aspx#S2Salas
Interesting, I looked this up again and got a different answer: msdn.microsoft.com/en-us/library/… Not sure which is right :)Darnell
I think the answer from Brian S. here clears up the confusion: #6051429Salas
B
4

They are stateless by default and I would highly recommended keeping it that way if possible. However if for some reason you are unable to, you can enable state.

You have to use wsHttpBinding or wsDualHttpBinding and then set the SessionMode of the ServiceContract (MSDN link) to allowed or required. goes into much greater detail.

Brocade answered 18/5, 2011 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.