ASP.NET Core MVC – Start session extension modularized

I got a feature request that I should store something on session start. In the old ASP.NET Framework world it was easy because we had Globbal.asax.cs and its Session_Start event. The original answer to this question is here. But, as I prefer overengineering modular architecture and Sitecore Helix principles I wanted to make it extensible without touching this Foundation piece of code. As you probably know we have Startup.cs instead of Global.asax.cs and DistributedSessionStore. To extend the Create method of the DistributedSessionStore you can just simply inherit from its interface ISessionStore.

As its seen above I have implemented a custom interface which contains all the actions I want to call on Create. These 2 classes lays in the Foundation layer if you follow Sitecore Helix principles.

As a last but one, a SessionExtensions (lays in the Foundation layer as well) implemented to put together the configuration of the session in one place:

As a last step call this method in the ConfigureServices method and setup the application to use session – this lays in the Project layer.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s