November Happy Hour will be moved to Thursday December 5th.

Tobias
Aug 25, 2020
  1992
(1 votes)

Using the ASP.Net Async SessionState Module together with Episerver Visitor Groups

The ASP.Net Async SessionState Module has several benefits over the older default one as it is fully async.

To use it in an Episerver site is mostly plug and play, just follow the instructions on Microsoft dev blog and your are good to go. There is one scenario I have found were it doesn't work and that is with Visitor Group criterias that use the session as a storage of state. When registering the session-based criterias they look for the default session state module and if it cannot find it, the criterias will not work. One of these criteras is Landing URL / starting URL, which is used for determining what page on the site the user first visited.

In order to support the Async module we will have to basically redo what Episerver has done, but switch out the default module for the Async module.

First we create a IInitializationHttpModule and in it we initialize our own State storage handler, as Episervers' VisitorGroupStateStorageHandler only works for the default session stage module.

/// <remark> Taken from Epis VisitorGroupHttpInitialization, to support SessionStateModuleAsync </remark>
[InitializableModule]
public class AsyncVisitorGroupHttpHandler : IInitializableHttpModule
{
    private AsyncVisitorGroupStateStorageHandler _storageHandler;

    public void Initialize(InitializationEngine context)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }

    public void InitializeHttpEvents(HttpApplication application)
    {
        Create().Initialize(application.Modules, CriterionEvents.Instance);
    }

    private AsyncVisitorGroupStateStorageHandler Create()
    {
        if (_storageHandler == null)
        {
            _storageHandler = new AsyncVisitorGroupStateStorageHandler();
        }

        return _storageHandler;
    }
}

Now we create or state storage handler. Here the important part is that we go through all modules registered and find the SessionStateModuleAsync module so we can hook into session start event. The Visitor group criteria will listen to this, among other events, and for example store the landing URL of the user.

/// <remark> This is taken from Epis own implementation of VisitorGroupStateStorageHandler, but made to support SessionStateModuleAsync </remark>
public class AsyncVisitorGroupStateStorageHandler
{
    static ILogger _log = LogManager.GetLogger(typeof(AsyncVisitorGroupStateStorageHandler));
    public void Initialize(HttpModuleCollection application, ICriterionEventsRaiser instance)
    {
        var sessionModule = (SessionStateModuleAsync) application.AllKeys.Select(k => application[k])
            .SingleOrDefault(module =>
                module is SessionStateModuleAsync);
        if (sessionModule != null)
        {
            sessionModule.Start += (sender, args) =>
            {
                instance.RaiseStartSession(sender,
                    new CriterionEventArgs(new HttpContextWrapper(HttpContext.Current)));
            };
        }
        else
        {
            _log.Warning("No SessionStateModuleAsync found, criterias relying on session state will not work");
        }
    }
}

With this code in place we should now see our visitor groups working as intended.

Aug 25, 2020

Comments

Daniel Zwolinski
Daniel Zwolinski Sep 1, 2020 09:17 AM

Awesome post, thanks!

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog