Saturday, April 23, 2016

Using Sitecore xDB with a ASP.NET Handler (ashx file)

In Sitecore using xDB requires a Session in order to maintain the context as I have mentioned in my previous posts about using xDB without an active Session.  While it is possible in a handler to persist data without the Session, it is definitely preferred as it is far more reliable than any methods you can use to work around it.

By default in .NET a handler does not maintain the Session as it is intended to be very light weight. This is very easy to resolve though by simply making it inherit IReadOnlySessionState or IRequiresSessionState.  Once your handler inherits one of those interfaces it will get the Session and xDB will work.