/// <summary>
        /// Checks if any of the hosted identities expired.
        /// If so, it deletes them.
        /// </summary>
        public void CheckExpiredHostedIdentities()
        {
            log.Trace("()");

            DateTime      now            = DateTime.UtcNow;
            List <byte[]> imagesToDelete = new List <byte[]>();

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                // Disable change tracking for faster multiple deletes.
                unitOfWork.Context.ChangeTracker.AutoDetectChangesEnabled = false;

                DatabaseLock lockObject = UnitOfWork.HostedIdentityLock;
                unitOfWork.AcquireLock(lockObject);
                try
                {
                    List <HostedIdentity> expiredIdentities = unitOfWork.HostedIdentityRepository.Get(i => i.ExpirationDate < now, null, true).ToList();
                    if (expiredIdentities.Count > 0)
                    {
                        log.Debug("There are {0} expired hosted identities.", expiredIdentities.Count);
                        foreach (HostedIdentity identity in expiredIdentities)
                        {
                            if (identity.ProfileImage != null)
                            {
                                imagesToDelete.Add(identity.ProfileImage);
                            }
                            if (identity.ThumbnailImage != null)
                            {
                                imagesToDelete.Add(identity.ThumbnailImage);
                            }

                            unitOfWork.HostedIdentityRepository.Delete(identity);
                            log.Debug("Identity ID '{0}' expired and will be deleted.", identity.IdentityId.ToHex());
                        }

                        unitOfWork.SaveThrow();
                        log.Debug("{0} expired hosted identities were deleted.", expiredIdentities.Count);
                    }
                    else
                    {
                        log.Debug("No expired hosted identities found.");
                    }
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                }

                unitOfWork.ReleaseLock(lockObject);
            }


            if (imagesToDelete.Count > 0)
            {
                ImageManager imageManager = (ImageManager)Base.ComponentDictionary["Data.ImageManager"];

                foreach (byte[] hash in imagesToDelete)
                {
                    imageManager.RemoveImageReference(hash);
                }
            }


            log.Trace("(-)");
        }