public static IOrganizationSettingsData GetOrganizationSettingsData(OrganizationId organizationId, IAirSyncContext context)
        {
            if (organizationId == null)
            {
                throw new ArgumentException("OrganizationId is null");
            }
            if (!ADNotificationManager.started)
            {
                throw new InvalidOperationException("ADNotificationManager should be started first!");
            }
            IConfigurationSession scopedSession     = null;
            ADObjectId            configurationUnit = organizationId.ConfigurationUnit;

            if (configurationUnit == null)
            {
                if (ADNotificationManager.enterpriseConfigurationID == null)
                {
                    scopedSession = ADNotificationManager.CreateScopedADSession(organizationId, ConsistencyMode.IgnoreInvalid);
                    ADNotificationManager.enterpriseConfigurationID = scopedSession.GetOrgContainerId();
                    if (context != null)
                    {
                        context.ProtocolLogger.SetValue(ProtocolLoggerData.DomainController, scopedSession.LastUsedDc);
                    }
                }
                configurationUnit = ADNotificationManager.enterpriseConfigurationID;
            }
            ADNotificationManager.ADSettingsInfo <IOrganizationSettingsData> adsettingsInfo = null;
            AirSyncDiagnostics.TraceDebug <ADObjectId>(ExTraceGlobals.RequestsTracer, null, "Loaded ConfigurationUnit {0}", configurationUnit);
            Func <IOrganizationSettingsData> loadDataAction = () => ADNotificationManager.LoadOrganizationSettings(scopedSession ?? ADNotificationManager.CreateScopedADSession(organizationId, ConsistencyMode.PartiallyConsistent), context);

            ADNotificationManager.LoadADSettingsData <IOrganizationSettingsData>(ADNotificationManager.organizationSettingsCache, configurationUnit.DistinguishedName, loadDataAction, organizationId.PartitionId, out adsettingsInfo);
            return(adsettingsInfo.ADSettingsData);
        }
        private static bool LoadADSettingsData <T>(MruDictionaryCache <string, ADNotificationManager.ADSettingsInfo <T> > cachedData, string adObjectDN, Func <T> loadDataAction, PartitionId partitionId, out ADNotificationManager.ADSettingsInfo <T> adSettingDataInfo)
        {
            bool result = false;

            if (GlobalSettings.DisableCaching || !cachedData.TryGetValue(adObjectDN, out adSettingDataInfo) || adSettingDataInfo.WhenCreated.Add(GlobalSettings.ADCacheExpirationTimeout) < ExDateTime.UtcNow)
            {
                lock (ADNotificationManager.lockObject)
                {
                    if (GlobalSettings.DisableCaching || !cachedData.TryGetValue(adObjectDN, out adSettingDataInfo) || adSettingDataInfo.WhenCreated.Add(GlobalSettings.ADCacheExpirationTimeout) < ExDateTime.UtcNow)
                    {
                        AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, null, "Load user settings from AD for DN {0}", adObjectDN);
                        T adSettings = loadDataAction();
                        adSettingDataInfo = new ADNotificationManager.ADSettingsInfo <T>(partitionId, adSettings, ExDateTime.UtcNow);
                        result            = true;
                    }
                    else
                    {
                        AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, null, "Load user adsettings from cache2 for DN {0}", adObjectDN);
                    }
                    return(result);
                }
            }
            AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, null, "Load user adsettings from cache for DN {0}", adObjectDN);
            return(result);
        }
 private static bool IsOrganizationSettingsAlreadyLoaded(ADObjectId organizationConfigurationUnitId)
 {
     ADNotificationManager.ADSettingsInfo <IOrganizationSettingsData> adsettingsInfo = null;
     return(ADNotificationManager.organizationSettingsCache.TryGetValue(organizationConfigurationUnitId.DistinguishedName, out adsettingsInfo));
 }