Documentation Center

Sample Unity dependency injection configuration file and Web.config update

This sample configuration file shows how to configure dependency injection. You also see how to update Web.config.

To perform dependency injection, use the dependency injection container called Microsoft Unity. For more information, refer to this link: https://msdn.microsoft.com/en-us/library/ff647202.aspx. Here is a sample Unity configuration file:
<?xml version="1.0"?>
<unity>
  <assembly name="Sdl.Web.Delivery.Core" />
  <namespace name="Sdl.Web.Delivery.Core" />
  <namespace name="Sdl.Web.Delivery.Core.Logging" />

  <assembly name="Sdl.Web.Delivery.Service" />
  <namespace name="Sdl.Web.Delivery.Service" />

  <assembly name="Sdl.Web.Delivery.DiscoveryService" />
  <namespace name="Sdl.Web.Delivery.DiscoveryService" />

  <assembly name="Sdl.Web.Delivery.ServicesCore" />
  <namespace name="Sdl.Web.Delivery.ServiceCore.ClaimStore" />
  
  <assembly name="TestUnityCIL" />
  <namespace name="TestUnityCIL.CILProvidersImpl" />

  <typeAliases>
    <!-- Lifetime manager types -->
    <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
    <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
  </typeAliases>

  <!-- Unity Configuration -->
  <containers>
    <container name="sdl.web.delivery">
      <!-- Map the implementations to the interfaces -->
      <types>
        <type type="ILogger" mapTo="LogProvider">
          <lifetime type="singleton" />
        </type>
        <type type="IServiceCacheProvider" mapTo="CacheProvider">
          <lifetime type="singleton" />
        </type>
        <type type="IApplicationConfigurationReader" mapTo="AppConfigProvider">
          <lifetime type="singleton" />
        </type>
        <type type="IADFContextProvider" mapTo="ADFContextProvider">
          <lifetime type="singleton" />
        </type>
      </types>
    </container>
  </containers>
</unity>
To use this configuration, save it as Unity.config and make the following changes to your Web.config file:
  • In the <configSections> section, add the following element: <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  • At the end of the <configuration> section, add the following element: <unity configSource="Unity.config" />