Web Service Session Trouble
I had my first attempt at creating a web service lately. Everything went fairly smooth. I stuck to the Web Service Software Factory pattern in the MS patterns and practices, but didn’t use the actually code generation.
I got stuck when trying to use Session in the service. What a pain. First I needed to enable session state in the web.config. Ok. That’s a given.
Next I needed to set EnableSessionState = true in the WebMethodAttribute for every method that needs session. Ok. Not too bad. It’s fairly well documented, and was easy to find.
This next step is what stumped me. I still couldn’t get Session to work for the life of me. I found that every time a service method was called, the Session of the service had a new SessionID. This was driving me nuts. I searched for a while and found a few people saying to use CookieContainer, and a couple examples. None of the example made much sense, and they all looked like they were trying to save the service session across postbacks of the client application.
I decided that I would look into CookieContainer on MSDN since a few people mentioned that needed to be used. BINGO! This solved my problem.
Basically, you create a CookieContainer on the proxy of your service. Then when you request something from the service, the service has a place to save it’s sesssion (the CookieContainer).
I don’t know why this isn’t and can’t be built in.
Oh well. Now I know better.