private static GetStorageLookupDataResponse GetStorageLookup(GetStorageLookupDataRequest request)
            {
                // initialize caching storage
                DataStoreManager.InstantiateDataStoreManager(request.RequestContext);

                StorageLookupDataManager         dataManager   = new StorageLookupDataManager(request.RequestContext);
                Dictionary <long, StorageLookup> lookupMapping = dataManager.GetStorageLookupMapping(request.ConnectionString);

                return(new GetStorageLookupDataResponse(lookupMapping));
            }
            private GetConnectionStringResponse GetConnectionString(GetConnectionStringRequest request)
            {
                // Get current channel identifier.
                GetChannelIdServiceRequest channelIdRequest = new GetChannelIdServiceRequest();
                ICommerceRuntime           runtime          = request.RequestContext.Runtime;
                long channelId = runtime.Execute <GetChannelIdServiceResponse>(channelIdRequest, request.RequestContext, skipRequestTriggers: true).ChannelId;

                // If connection string is overriden simply return connection string from configuration and skip lookup.
                string connectionStringFromConfig = runtime.Configuration.ConnectionString;

                if (runtime.Configuration.IsConnectionStringOverridden)
                {
                    if (string.IsNullOrWhiteSpace(connectionStringFromConfig))
                    {
                        throw new ConfigurationException(
                                  ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidChannelConfiguration,
                                  string.Format("No connection string found for channel ({0}).", channelId));
                    }

                    return(new GetConnectionStringResponse(connectionStringFromConfig));
                }

                // For current channel identifier query storage lookup data.
                GetStorageLookupDataRequest  storageLookupRequest  = new GetStorageLookupDataRequest(connectionStringFromConfig);
                GetStorageLookupDataResponse storageLookupResponse = request.RequestContext.Runtime.Execute <GetStorageLookupDataResponse>(storageLookupRequest, request.RequestContext, skipRequestTriggers: true);

                if (!storageLookupResponse.LookupValues.ContainsKey(channelId))
                {
                    throw new ConfigurationException(
                              ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidChannelConfiguration,
                              ExceptionSeverity.Warning,
                              string.Format("The specified channel ({0}) was not found.", channelId));
                }

                // Using storage identifier from storage lookup view lookup connection string in configuration.
                StorageLookup lookup = storageLookupResponse.LookupValues[channelId];
                string        connectionString;

                if (!runtime.Configuration.StorageLookupConnectionStrings.TryGetValue(lookup.StorageId, out connectionString))
                {
                    throw new ConfigurationException(
                              ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidChannelConfiguration,
                              ExceptionSeverity.Warning,
                              string.Format("The connection string is not found. StorageId: {0}. ChannelId: {1}.", lookup.StorageId, channelId));
                }

                return(new GetConnectionStringResponse(connectionString));
            }
            /// <summary>
            /// Gets the channel identifier by operating unit number.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="operatingUnitNumber">The operating unit number.</param>
            /// <returns>The channel identifier.</returns>
            private static long GetChannelIdByOperatingUnitNumber(RequestContext context, string operatingUnitNumber)
            {
                GetStorageLookupDataRequest request = new GetStorageLookupDataRequest(context.Runtime.Configuration.ConnectionString);
                IEnumerable <StorageLookup> lookups = context.Execute <GetStorageLookupDataResponse>(request).LookupValues.Values;

                foreach (StorageLookup lookup in lookups)
                {
                    if (string.Equals(operatingUnitNumber, lookup.OperatingUnitNumber, StringComparison.OrdinalIgnoreCase))
                    {
                        return(lookup.ChannelId);
                    }
                }

                string message = string.Format("The specified default operating unit ({0}) was not found.", operatingUnitNumber);

                throw new ConfigurationException(ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_ConfigurationSettingNotFound, message);
            }
            /// <summary>
            /// Gets the channel identifier of the first published channel in local database.
            /// </summary>
            /// <param name="context">Request context.</param>
            /// <returns>The channel identifier.</returns>
            /// <remarks>
            /// This is designed for Retail Server scenarios of Device Activation and Restricted Logon.
            /// In order to make the system become more robust, ONLY published channel is allowed to be used.
            /// </remarks>
            private static long GetFirstPublishedChannelId(RequestContext context)
            {
                GetStorageLookupDataRequest request = new GetStorageLookupDataRequest(context.Runtime.Configuration.ConnectionString);
                IEnumerable <StorageLookup> lookups = context.Execute <GetStorageLookupDataResponse>(request).LookupValues.Values;

                IEnumerable <StorageLookup> publishedChannels = lookups.Where(lookup => lookup.IsPublished).ToList();

                if (!publishedChannels.Any())
                {
                    string errorMessage = "The published channel can not be found in local database. Please make sure at least 1 retail channel is published to this DB through AX.";
                    throw new ConfigurationException(ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_ConfigurationSettingNotFound, errorMessage);
                }

                // Favors channel(s) published to local, in order to avoid database connection cross different machines.
                var firstPublishedChannelOnLocal =
                    publishedChannels.FirstOrDefault(lookup => lookup.IsLocal);

                if (firstPublishedChannelOnLocal != null)
                {
                    return(firstPublishedChannelOnLocal.ChannelId);
                }

                return(publishedChannels.FirstOrDefault().ChannelId);
            }