I was trying today to access the session state inside a http handler but just kept getting a null references exception? A null reference when trying to add an item to the session? Surely the session always exists across every request made by a user? Well it does but not by default when trying to access it inside a http handler.

To allow the session to be accessible inside a http handler you will need to implement either IReadOnlySessionState or IRequiresSessionState. It will depend on what you need to do with the session as to which one you implement. If you need read access use IReadOnlySessionState but if you need read and write access use IRequiresSessionState.
I was adding an object to the session so I needed both read and write access so I implemented IRequiresSessionState (see line 7). After doing so there were no further issues accessing the session inside the http handler.
namespace WebApplication1
{
public class Naughty : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Session.Add("PostedValue", 1);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Here is a very simple example of how to build a simple ASP.NET web service using VS2008 (FYI most of the popup helpers you see inside the IDE is Resharper a very cool plug-in for Visual Studio).
I am calling my web service from a console application but remember it could just as easily be called from any application type WPF / Silverlight / Web Application etc.
Go full screen its much better :->