private IClarifySession getSession(string username, bool isConfigured = true, bool isObserved = true)
{
IClarifySession session;
using (_logger.Push("Get session for {0}.".ToFormat(username)))
{
var success = _agentSessionCacheByUsername.TryGetValue(username, out session);
if (success)
{
if (_clarifyApplication.IsSessionValid(session.Id))
{
_logger.LogDebug("Found valid session in cache.");
return session;
}
_logger.LogDebug("Ejecting invalid session.");
EjectSession(username, isObserved);
}
ClarifySession clarifySession;
lock (_agentSessionCacheByUsername)
{
if (_agentSessionCacheByUsername.ContainsKey(username))
{
_logger.LogDebug("Found session (within the lock). Assuming it is valid because it must be very recent.");
return _agentSessionCacheByUsername[username];
}
//session = CreateSession(username, isConfigured, isObserved);
_logger.LogDebug("Creating missing session.");
clarifySession = _clarifyApplication.CreateSession(username, ClarifyLoginType.User);
clarifySession.SetNullStringsToEmpty = true;
session = wrapSession(clarifySession);
_logger.LogInfo("Created session {0}.".ToFormat(clarifySession.SessionID));
_agentSessionCacheByUsername.Add(username, session);
_logger.LogDebug("{0} sessions are now in the cache.", _agentSessionCacheByUsername.Count);
}
visitSession(clarifySession, isConfigured, isObserved);
}
return session;
}