Loading...
Area: Episerver DXC Service
Applies to versions: Not applicable

Environment configurations

This topic describes various environment-specific configuration options in Episerver Digital Experience Cloud Service, such as restricting access, or applying specific configurations or configuration transforms, when deploying.

In this topic

Environment-specific configurations

When you deploy to a production environment, ensure that the correct configurations are applied. In a Production environment, you should never use credentials, tokens, or endpoints from a Preproduction environment.

Environment-specific configurations are also useful if you have an e-commerce site with payment provider methods that you do not want to run in a test environment, or if you want to apply specific log rules for an environment.

Adding the configuration

Add the environment-specific configuration in web.config for your web application. Alternatively, you can store the settings in the database and retrieve the settings based on environment name by adding the specific configurations for the environments (integration, preproduction, production) where you want them to apply.

Note: See the Azure portal to identify the environment names, see Accessing Azure information to get access.

Identifying the environment

To apply a specific configuration, you need to identify the environment name at runtime using episerver:EnvironmentName. The defined name for each environment setting can be retrieved from Application settings in Azure Portal.

Add the configuration for each environment requiring a specific configuration.

Using the configuration

To apply a specific configuration and identify the environment at runtime, use the following code:

ConfigurationManager.AppSettings["episerver:EnvironmentName"]

Applying this where needed picks the correct configuration for the running environment. 

var appSettings = ConfigurationManager.AppSettings;
var environmentName = appSettings["episerver:EnvironmentName"];
if (environmentName == null || environmentName.Equals("Integration"))
{
    // TODO: use Integration configuration parameters
}
else if (environmentName.Equals("Preproduction"))
{
    // TODO: use Preproduction configuration parameters
}
else if (environmentName.Equals("Production"))
{
    // TODO: use Production configuration parameters                
}

Defining site context

The following example defines and uses specific payment gateways and shipping methods for environments in an Episerver Commerce solution based on Digital Experience Cloud (DXC).

Create a class to act as a global context for the site.

using EPiServer.ServiceLocation;
using System;
using System.Configuration;
 
namespace EPiServer.DxcSite
{
    [ServiceConfiguration(Lifecycle = ServiceInstanceScope.Singleton)]
    public class DxcApplicationContext
    {
        private DxcEnvironment _environment;
        public DxcApplicationContext()
        {
            var setting = ConfigurationManager.AppSettings.Get(“episerver:EnvironmentName”);
            if(!Enum.TryParse<DxcEnvironment>(setting, true, out _environment))
            {
                //Default environment is integration;
                _environment = DxcEnvironment.Integration;
            }
        }
 
        public DxcEnvironment CurrentEnvironment
        {
            get
            {
                return _environment;
            }
        }
    }
 
    public enum DxcEnvironment
    {
        Integration,
        Preproduction,
        Production
    }
}

Defining environment-specific payment gateways and shipping methods

Currently, the best way to separate payment gateways and shipping methods is to use a specific prefix for their system names. For example, you can set Integration- for the payment gateways and shipping methods used in Integration, Preproduction- for Preproduction and so on. The prefix should be in line with the enum defined in previous steps, for easier matching.

Getting payment gateways and shipping methods

public IEnumerable<ShippingMethodDto.ShippingMethodRow> FilterShippingMethods(ShippingMethodDto dto)
{
    var environment = ServiceLocator.Current.GetInstance<DxcApplicationContext>().CurrentEnvironment;
    return dto.ShippingMethod.Select().Where(c => c["Name"].ToString().StartsWith(environment.ToString(),
     StringComparison.OrdinalIgnoreCase))
        .Select(c => (ShippingMethodDto.ShippingMethodRow)c);
} 

Note: The code that displays the shipping method name to the user in the production environment might need modification to remove the prefix part.

Configuration transforms

DXC Service supports the use of configuration transforms to make changes to .config-files when deploying between environments. Even though the following example is for a web.config file, DXC Service supports this feature on any *.config file if the transform file follows the naming convention (OriginalFileName.episerver:EnvironmentName.config).

Note: If you deploy using Deployment API, you can deploy the code package to the Integration environment directly instead of the conventional way of publishing the code from Visual Studio. However, when you do so, configuration transformation is applied on the code being deployed to Integration (if the deployment engine sees a valid transformation file to apply). 

For example, if there is a web.Integration.config, it is processed as part of the deployment flow. This is similar to when you deploy code from Integration to another environment using the DXC service management portal. Deploying to Integration using Visual Studio does not apply any configuration transformation. 

Adding transform files

Add a file called Web.episerver:EnvironmentName.config (see Identifying the environment for naming conventions) at the same level/folder as the web.config file you want to transform. If you have web.config files at multiple levels/folders that needs transformation, add a transform file next to each one.

The episerver:EnvironmentName-part should correspond to the value of the application setting with the same name of the target environment for the deployment. For example, to apply a transformation file to web.config when deploying to the Preproduction environment, you add a file called web.Preproduction.config that contains the transformation you need.

Note: If you need to do configuration transformations in multiple environments (such as for both Preproduction and Production), then use multiple transformation files, which are applied one after the other as the deployment moves towards the production environment.

Also, if you deploy using Deployment API, you can do the multiple configuration transformation only when you deploy the code from a source environment to the target; you cannot deploy using code packages. 

Example:

  1. The application is deployed to the Integration environment. It contains a web.config for that environment (no transformation is done in Integration), a web.Preproduction.config, and a web.Production.config.
  2. The application is deployed to Preproduction and the web.Preproduction.config transformation file is applied to the web.config used in Integration, creating a new web.config in Preproduction.
  3. The application gets deployed to Production, and the web.Production.config file gets applied to the web.config from Preproduction.

    The Production web.config is therefore the result of both web.Preproduction.config and web.Production.config being applied to the original web.config file that was used in the Integration environment. The transformation files need to follow the syntax described in Web.config Transformation Syntax (Microsoft).

To make this work from Visual Studio you need to make sure you add the transformation files as content files so they are actually deployed together with the rest of the site.

Adding transform files for Commerce Manager link

When deploying Commerce Manager between different environments, the link remains the same across these. Therefore, in some cases it will be pointing to the wrong one. As previously explained, you can use transform files, which takes care of updating Commerce Manager links specifically for each environment.

For example, for deploying to Preproduction a transform file could look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
	<appSettings>
		<add key="CommerceManagerLink" value="{LinkToCommerceManager}"
                  xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
	</appSettings>
</configuration>

Custom maintenance page

During deployments where the site needs to be taken offline, you have the option to add a custom maintenance page that will be displayed during the deploy. See Custom maintenance page how to work with maintenance pages.

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 11, 2018