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)); }
private static RequestContext CreateRequestContext(string staffId, Device device, ICommerceRuntime runtime) { CommerceIdentity identity; if (string.IsNullOrWhiteSpace(staffId)) { identity = new CommerceIdentity(device); identity.Roles.Add(CommerceRoles.Anonymous); } else { var employee = new Employee() { StaffId = staffId }; identity = new CommerceIdentity(employee, device); } CommercePrincipal principal = new CommercePrincipal(identity); RequestContext context = new RequestContext(runtime); context.SetPrincipal(principal); return(context); }