We have multi-step form hosted in sitecore 8.2. As user fills up each step and click on next, our custom pipeline gets invoked. For each steps we have corresponding pipelines. Each pipeline updates the session data as user progresses.
Problem:
During performance test, we noticed that if a user clicks the next button exactly at same time the their session data gets overwritten with other user's data.
Came across this blog post which talks about Processors being Singlon Lifecycle: https://ctor.io/be-careful-with-sitecore-pipeline-processors-lifecycle/
Questions:
Why pipelines are designed to be singleton lifecycle? This issue only occurs if more than 1 user happens the click next button exactly at same time.
Code snippet
namespace MyPipeline
{
public class Initialize
{
private readOnly ISessionStore _sessionStore;
public Initialize(ISessionStore sessionStore)
{
this._sessionStore = sessionStore;
Sitecore.Diagnostics.Log.Info("ctor() has been called", this);
}
public override void Process(EmployeePipelineArgs args)
{
_sessionStore.SetSessionData(EmployeePipelineArgs);
}
}
}
namespace MyPipeline
{
public class SessionTest
{
private readOnly ISessionStore _sessionStore;
public LifecycleTest(ISessionStore sessionStore)
{
this._sessionStore = sessionStore;
Sitecore.Diagnostics.Log.Info("ctor() has been called", this);
}
public override void Process(CustomPipelineArgs args)
{
var mySessionData = _sessionStore.GetSessionData()
Sitecore.Diagnostics.Log.Info(mySessonData.Name);
}
}
}
Snippet for SetSessionData
public class SessionStore : ISessionStore
{
private readonly IHttpContextWrapper _httpContext;
public SessionStore(IHttpContextWrapper httpContext)
{
_httpContext = httpContext;
}
public override void SetSessionData(data)
{
_httpContext.Current.Session[somekey] = data;
}
}
public class HttpContextWrapper : IHttpContextWrapper
{
public HttpRequest Request => HttpContext.Current.Request;
public HttpResponse Response => HttpContext.Current.Response;
public HttpContext Current => HttpContext.Current;
}
Configuration
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
</pipelinetest>
<processor type="MyPipeline.Initialize, MyPipeline" resolve ="true" />
<processor type="MyPipeline.SessionTest, MyPipeline" resolve ="true" />
</pipelinetest>
</pipelines>
</sitecore>
</configuration>
Thanks.
ISessionStore
methodSetSessionData
?