For last few weeks we faced a performance issue on our Sitecore production servers. Page response time was substantially increased by 50% and getting slowness in full page loading. While investigation we found that this was just because of our customized MediaRequestHandler.
Our requirement was to display some Disclaimer Page when a user request for some Sitecore media files (not for any image), after accepting disclaimer we can continue to download requested media. All other media items should be served directly.
We used session for managing this disclaimer. For using session in the MediaRequestHandler, we implemented iRequiresSessionState interface in it. See below code snippet
So, what the reason for the slower page rendering? Yes, that's IRequiresSessionState which was a halts in performance.
As per our requirement, we have to show Dislaimer for selected Sitecore Media Files only (Not for any Media Images), we thought to create a HttpModule, which will check whether the request is for media files or images. If it is a file, then enable session state and if it is an image, disable session state.
And now we checked Page Response time, now feeling heavenly. All problem gone, Page Response Time now dropped down to earlier stage.
Our requirement was to display some Disclaimer Page when a user request for some Sitecore media files (not for any image), after accepting disclaimer we can continue to download requested media. All other media items should be served directly.
We used session for managing this disclaimer. For using session in the MediaRequestHandler, we implemented iRequiresSessionState interface in it. See below code snippet
class CustomRequestHandler : Sitecore.Resources.Media.MediaRequestHandler, IRequiresSessionState { protected override bool DoProcessRequest(HttpContext context) { if (context.Request.Url.ToString().ToLower().Contains("/~/media/files/")) { // Check session, if session does not available, then send it to disclaimer // Once user accepts Disclaimer, generate a session with user details. } else { // Proceed all image requests base.DoProcessRequest(...); } } }
So, what the reason for the slower page rendering? Yes, that's IRequiresSessionState which was a halts in performance.
Can IRequiresSessionState really make my page slower?
Below image shows how ASP.NET Session state works:
Now we can definitely say YES, it can slow down. State access requires that the aspx pages or handler, run sequentially. Each page/handler may read from and write to the session state. To handle this without creating any errors in the session state, .NET will only run one at a time using locking mechanism, so no handlers will run when the aspx page itself is running.
Tactics:
Tactics:
- Can we customize handler not to use session state?
- No, this will disable session so we cannot achieve disclaimer functionality - We can decide the request needs session or not and use session dynamically.
- Is it possible? Yes, it is, using HttpModules
Solved by Dynamically deciding Session State Behavior programmatically
Changing ASP.NET session state behavior dynamically is possible only using HttpModules or Global.asax. It is not possible in HttpHandlers or other code behind. This solution is possible only in .NET 4.0 and later. Yes, we can define it at Page Directives but that's static.
As per our requirement, we have to show Dislaimer for selected Sitecore Media Files only (Not for any Media Images), we thought to create a HttpModule, which will check whether the request is for media files or images. If it is a file, then enable session state and if it is an image, disable session state.
Step-1:
We removed IRequiresSessionState from our CustomMediaRequestHandler, so it will not use Session State directly.Step-2:
Created HttpModule, which will decide whether to use Session State or not.public class CustomMediaRequestSessionModule : IHttpModule { public void Init(HttpApplication application) { application.BeginRequest += (new EventHandler(this.Application_BeginRequest)); } private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; var CurrentContext = HttpContext.Current; if (CurrentContext.Request.Url.ToString().ToLower().Contains("/~/media/files/")) { // Files need Disclaimer functionality, so session needed CurrentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required); } else if (CurrentContext.Request.Url.ToString().ToLower().Contains("/~/media/images/")) { // Images do not need Disclaimer, so no session needed CurrentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Disabled); } } public void Dispose() { } }
Step-3:
Now, in web.config, register this module under system.webServer/modules section:<add type="SitecoreTactics.Resources.Media.CustomMediaRequestSessionModule, SitecoreTactics" name="CustomMediaRequestSessionModule" />
And now we checked Page Response time, now feeling heavenly. All problem gone, Page Response Time now dropped down to earlier stage.