Session State in an HttpModule

Thursday, January 22nd, 2009

This can be achieved with a little jiggery pokery. I found this on the internets:

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Diagnostics;

// This code demonstrates how to make session state available in HttpModule,
// regradless of requested resource.
// author: Tomasz Jastrzebski

public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
}

void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;

if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) {
// no need to replace the current handler
return;
}

// swap the current handler
app.Context.Handler = new MyHttpHandler(app.Context.Handler);
}

void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;

MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler;

if (resourceHttpHandler != null) {
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}

// -> at this point session state should be available

Debug.Assert(app.Session != null, “it did not work :(”);
}

public void Dispose()
{

}

// a temp handler used to force the SessionStateModule to load session state
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;

public MyHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}

public void ProcessRequest(HttpContext context)
{
// do not worry, ProcessRequest() will not be called, but let’s be safe
throw new InvalidOperationException(”MyHttpHandler cannot process requests.”);
}

public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}

The only thing that you also have to remember is to move the code that you are executing that requires session state to an event that occurs after the session has been made avaliable. For example BeginRequest just won’t do! Move it to something like ‘PreRequestHandlerExecute’.

Synergy - the must for my growing desktop!

Tuesday, January 20th, 2009

This seems like the solution to having at least two machines on my desk - something that is happening more and more these days!

I have seen it in action before but the latest example that I have seen really impressed me - it even worked!

Lets give it a go!

http://synergy2.sourceforge.net/

Session-state modes ’state server’

Wednesday, January 14th, 2009

Going to do a bit of reading about session state modes…..

http://msdn.microsoft.com/en-us/library/ms178586.aspx

Windsor log4net facility

Tuesday, January 13th, 2009

http://devlicio.us/blogs/casey/archive/2008/06/18/logging-with-castle-windsor-the-logging-facility-and-log4net.aspx

WindsorContainer c = new WindsorContainer();
ILoggingFacility facility = new LoggingFacility(LoggerImplementation.Console);

c.AddFacility(”logging”, facility);

c.Resolve<ILoggerFactory>().Create(”name of your logger”);
// Or use type name of current class
c.Resolve<ILoggerFactory>().Create(GetType());

http://www.castleproject.org/container/facilities/v1rc3/logging/index.html

Editing FLV’s the easy way…

Thursday, January 8th, 2009

This is a nice AIR app that allows you to edit FLV’s

http://www.richapps.de/?p=48