コード例 #1
0
        private static void CreateUserContext(HttpContext httpContext, UserContextKey userContextKey, AuthZClientInfo effectiveCaller, out IMailboxContext userContext, out UserContextStatistics userContextStats)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            userContextStats = new UserContextStatistics();
            userContext      = null;
            OwaIdentity owaIdentity  = null;
            OwaIdentity owaIdentity2 = null;
            OwaIdentity owaIdentity3 = null;

            try
            {
                try
                {
                    OwaIdentity owaIdentity4 = OwaIdentity.ResolveLogonIdentity(httpContext, effectiveCaller);
                    owaIdentity2 = owaIdentity4;
                    string explicitLogonUser = UserContextUtilities.GetExplicitLogonUser(httpContext);
                    if (!string.IsNullOrEmpty(explicitLogonUser))
                    {
                        ExTraceGlobals.UserContextTracer.TraceDebug <string>(0L, "Created partial mailbox identity from SMTP address={0}", explicitLogonUser);
                        owaIdentity  = OwaIdentity.CreateOwaIdentityFromExplicitLogonAddress(explicitLogonUser);
                        owaIdentity3 = owaIdentity;
                    }
                    if (userContextKey == null)
                    {
                        userContextKey = UserContextKey.CreateNew(owaIdentity4, owaIdentity, httpContext);
                        ExTraceGlobals.UserContextTracer.TraceDebug <UserContextKey>(0L, "Creating new user context key: {0}", userContextKey);
                    }
                    else
                    {
                        ExTraceGlobals.UserContextTracer.TraceDebug <UserContextKey>(0L, "Reusing user context key: {0}", userContextKey);
                    }
                    OwaRWLockWrapper userContextKeyLock = UserContextManager.GetUserContextKeyLock(userContextKey.ToString());
                    if (userContextKeyLock == null)
                    {
                        userContextStats.Error = UserContextCreationError.UnableToAcquireOwaRWLock;
                        throw new OwaLockException("UserContextManger::CreateUserContext was not able to create a lock");
                    }
                    if (userContextKeyLock.LockWriterElastic(6000))
                    {
                        try
                        {
                            userContext = UserContextManager.GetMailboxContextFromCache(userContextKey, false);
                            if (userContext != null && userContext.TerminationStatus == UserContextTerminationStatus.TerminatePending)
                            {
                                UserContextManager.TerminateSession(userContext, userContext.AbandonedReason);
                                userContext = null;
                            }
                            if (userContext == null)
                            {
                                userContextStats.Created = true;
                                ExTraceGlobals.UserContextTracer.TraceDebug <UserContextKey>(0L, "User context was not found in the cache, creating one. UserContextKey: {0}", userContextKey);
                                bool flag = false;
                                try
                                {
                                    if (UserContextManager.IsSharedContextKey(userContextKey))
                                    {
                                        userContext = new SharedContext(userContextKey, httpContext.Request.UserAgent);
                                    }
                                    else
                                    {
                                        userContext = new UserContext(userContextKey, httpContext.Request.UserAgent);
                                    }
                                    Stopwatch stopwatch2 = Stopwatch.StartNew();
                                    userContext.Load(owaIdentity4, owaIdentity, userContextStats);
                                    userContextStats.LoadTime = (int)stopwatch2.ElapsedMilliseconds;
                                    UserContextManager.InsertIntoCache(httpContext, userContext);
                                    owaIdentity2 = null;
                                    owaIdentity3 = null;
                                    string userName = userContext.LogonIdentity.UserSid.ToString();
                                    PerformanceCounterManager.UpdatePerfCounteronUserContextCreation(userName, false, false, Globals.ArePerfCountersEnabled);
                                    flag = true;
                                }
                                finally
                                {
                                    if (!flag)
                                    {
                                        ExTraceGlobals.UserContextTracer.TraceDebug <UserContextKey>(0L, "User context creation failed. UserContextKey: {0}", userContextKey);
                                        if (userContext != null)
                                        {
                                            ExTraceGlobals.UserContextTracer.TraceDebug <UserContextKey>(0L, "Disposing user context. UserContextKey: {0}", userContextKey);
                                            userContext.Dispose();
                                            userContext.State = UserContextState.Abandoned;
                                        }
                                    }
                                }
                            }
                            goto IL_1EA;
                        }
                        finally
                        {
                            userContextKeyLock.ReleaseWriterLock();
                        }
                        goto IL_1D4;
IL_1EA:
                        goto IL_237;
                    }
IL_1D4:
                    userContextStats.Error = UserContextCreationError.UnableToAcquireOwaRWLock;
                    throw new OwaLockTimeoutException("UserContextManger::CreateUserContext was not able to acquire a rw lock", null, null);
                }
                catch (OwaIdentityException ex)
                {
                    userContextStats.Error = UserContextCreationError.UnableToResolveLogonIdentity;
                    OwaServerTraceLogger.AppendToLog(new TraceLogEvent("UserContext", userContext, "UserContextManager.CreateUserContext", ex.ToString()));
                    throw;
                }
                catch (Exception ex2)
                {
                    OwaServerTraceLogger.AppendToLog(new TraceLogEvent("UserContext", userContext, "UserContextManager.CreateUserContext", ex2.ToString()));
                    throw;
                }
                IL_237 :;
            }
            finally
            {
                if (owaIdentity2 != null)
                {
                    owaIdentity2.Dispose();
                }
                if (owaIdentity3 != null)
                {
                    owaIdentity3.Dispose();
                }
                stopwatch.Stop();
                userContextStats.AcquireLatency            = (int)stopwatch.ElapsedMilliseconds;
                httpContext.Items["UserContextStatistics"] = userContextStats;
            }
        }
コード例 #2
0
        private static IMailboxContext GetMailboxContextFromCache(UserContextKey userContextKey, bool lockUserContextKey)
        {
            ExTraceGlobals.UserContextCallTracer.TraceDebug(0L, "UserContextManager.TryGetUserContextFromCache");
            if (userContextKey == null)
            {
                ExTraceGlobals.UserContextCallTracer.TraceDebug(0L, "UserContextManager.TryGetUserContextFromCache: No UserContextKey provided, returning null.");
                return(null);
            }
            UserContextManager.UserContextCacheWrapper userContextCacheWrapper = null;
            IMailboxContext mailboxContext = null;
            bool            flag           = false;

            try
            {
                ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey>(0L, "Attempting to fetch user context from the cache.  Key={0}", userContextKey);
                userContextCacheWrapper = (UserContextManager.UserContextCacheWrapper)HttpRuntime.Cache.Get(userContextKey.ToString());
                if (userContextCacheWrapper != null && userContextCacheWrapper.UserContext != null)
                {
                    if (lockUserContextKey)
                    {
                        if (userContextCacheWrapper.Lock == null)
                        {
                            throw new OwaLockException("UserContextManager::GetUserContextFromCache cannot get a lock from UserContextCacheWrapper");
                        }
                        if (!userContextCacheWrapper.Lock.IsWriterLockHeld)
                        {
                            if (!userContextCacheWrapper.Lock.LockWriterElastic(3000))
                            {
                                throw new OwaLockTimeoutException("UserContextManager::GetUserContextFromCache cannot acquire a write lock in a time fashion");
                            }
                            flag           = true;
                            mailboxContext = userContextCacheWrapper.UserContext;
                            ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey, IMailboxContext>(0L, "User context found in cache. Key={0}, User context instance={1}", userContextKey, mailboxContext);
                        }
                    }
                    else
                    {
                        mailboxContext = userContextCacheWrapper.UserContext;
                        ExTraceGlobals.UserContextCallTracer.TraceDebug <UserContextKey, IMailboxContext>(0L, "User context found in cache. Key={0}, User context instance={1}", userContextKey, mailboxContext);
                    }
                }
                else
                {
                    ExTraceGlobals.UserContextCallTracer.TraceDebug <string>(0L, "An object for this user context ID value is not present in the cache (probably was expired), so we are going to reuse the user context ID value for the new session", userContextKey.UserContextId);
                }
            }
            finally
            {
                if (mailboxContext != null && mailboxContext.TerminationStatus == UserContextTerminationStatus.TerminatePending)
                {
                    UserContextManager.TerminateSession(mailboxContext, mailboxContext.AbandonedReason);
                    mailboxContext = null;
                }
                if (flag && userContextCacheWrapper != null && userContextCacheWrapper.Lock != null)
                {
                    userContextCacheWrapper.Lock.ReleaseWriterLock();
                }
                if (mailboxContext != null && mailboxContext.State != UserContextState.Active)
                {
                    mailboxContext = null;
                }
            }
            return(mailboxContext);
        }
コード例 #3
0
        private static void UserContextRemoved(object parameters)
        {
            bool flag = false;

            try
            {
                if (flag)
                {
                }
            }
            finally
            {
                try
                {
                    object[] array = parameters as object[];
                    string   text  = array[0] as string;
                    object   obj   = array[1];
                    CacheItemRemovedReason cacheItemRemovedReason = (CacheItemRemovedReason)array[2];
                    UserContextManager.UserContextCacheWrapper userContextCacheWrapper = obj as UserContextManager.UserContextCacheWrapper;
                    if (userContextCacheWrapper != null)
                    {
                        if (userContextCacheWrapper.UserContext != null)
                        {
                            userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Invoked.  Key=" + text);
                        }
                        if (userContextCacheWrapper.Lock != null && !userContextCacheWrapper.Lock.IsWriterLockHeld)
                        {
                            try
                            {
                                if (userContextCacheWrapper.Lock.LockWriterElastic(3000) && userContextCacheWrapper.UserContext != null)
                                {
                                    ExTraceGlobals.UserContextTracer.TraceDebug <string, IMailboxContext, int>(0L, "Removing user context from cache, Key={0}, User context instance={1}, Reason={2}", text, userContextCacheWrapper.UserContext, (int)cacheItemRemovedReason);
                                    userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Terminating.  Key=" + userContextCacheWrapper.UserContext.Key.ToString());
                                    UserContextManager.TerminateSession(userContextCacheWrapper.UserContext, cacheItemRemovedReason);
                                    userContextCacheWrapper.UserContext.LogBreadcrumb("UserContextRemoved Terminated.  Key=" + userContextCacheWrapper.UserContext.Key.ToString());
                                    userContextCacheWrapper.UserContext.TerminationStatus = UserContextTerminationStatus.TerminateCompleted;
                                }
                            }
                            finally
                            {
                                if (userContextCacheWrapper.Lock.IsWriterLockHeld)
                                {
                                    userContextCacheWrapper.Lock.ReleaseWriterLock();
                                }
                            }
                        }
                        if (userContextCacheWrapper.UserContext != null && userContextCacheWrapper.UserContext.TerminationStatus != UserContextTerminationStatus.TerminateCompleted)
                        {
                            userContextCacheWrapper.UserContext.TerminationStatus = UserContextTerminationStatus.TerminatePending;
                            userContextCacheWrapper.UserContext.AbandonedReason   = cacheItemRemovedReason;
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception ex)
                {
                    OwaDiagnostics.LogEvent(ClientsEventLogConstants.Tuple_UserContextTerminationError, string.Empty, new object[]
                    {
                        ex.ToString()
                    });
                    ExTraceGlobals.UserContextTracer.TraceDebug(0L, "Sending watson report");
                    ExWatson.SendReport(ex, ReportOptions.None, null);
                }
            }
        }