Web Services, Sessions and the Windows Service

For those that didn’t know, my beautiful wife is pregnant with our first child. The due date is the first week of November.  Because of this news I had to change jobs to make more money for my family. It was one of the hardest decisions I had to make as I loved working where I was. However, I’m having a blast working at my new job. I have always been a web developer. It was very rare when I needed to do any Windows Forms development.

I’m working on a cool program that I’m not going to tell you about. However, I have played with many cool pieces of .NET that I’ve used before but not in this context.

Most web programmers have used Web Services. I’ve used them a lot, but only in the web-web world.  You know, using JavaScript to call a web service, or call a web service from your code-behind. Very simple stuff. What I needed to do was write a web service that would be consumed from a windows service. However, I needed to enable Session on this web service so it was always in-sync with the windows service.

Enabling Sessions on a web service is very easy:

   77         [WebMethod(EnableSession=true)]

   78         public bool SessionExists()

   79         {

   80             if (Session["AgencyID"] == null)

   81                 return false;

   82             else

   83                 return true;

   84         }

However, getting the right cookie information to the windows service I’m writing was a bit difficult. Every web service object has the following object: CookieContainer.  This object basically eats and stores all cookies returned from any web service call (You don’t even need to pass anything back, it’s all automatic).

Here is how to use the CookieContainer object. This is a method from my windows service:

   81         private bool Authenticate()

   82         {

   83             

   84             

   85             try

   86             {

   87                 Authentication.Manager m = new Authentication.Manager(settings.AuthenticationMethod);

   88                 //Load a Cookie that was stored in an XML file from the Login Factory

   89                 System.Net.Cookie cookie = m.loadAuthCookie();

   90                 if (cookie == null) return false;

   91 

   92                 cc.Add(cookie);

   93                 Updater.Updater u = new Updater.Updater();

   94                 u.CookieContainer = cc;

   95                 if (u.SessionExists())

   96                     return true;

   97                 else

   98                     return false;

   99             }

  100             catch (Exception ex)

  101             {

  102                 throw new Exception("Failed to Load Authentication Cookie\n" + ex.Message);

  103             }

  104 

  105         }

The CookieContainer object must be instantiated before you make your web service call. Once you make your web service call and it completes, you will receive a session cookie. Here is the cookie in XML form (this is the format I use):

    1 <?xml version="1.0" encoding="utf-8"?>

    2 <authCookie>

    3     <name>ASP.NET_SessionId</name>

    4     <value>pae0be55uy2dw5z1wapmzp55</value>

    5     <domain>localhost</domain>

    6 </authCookie>

No Comments