Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Using Container Extensions

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

You can create your own custom container extensions, or use container extensions created by third parties, with the Unity Application Block. The application block includes a demonstration of a container extension that implements the Publish/Subscribe design pattern for events (for more information, see Walkthrough: The Unity Event Broker Extension QuickStart). Unity also uses default container extensions to implement its own functionality.


Ff647448.note(en-us,PandP.10).gifNote:
For details of how to create a container extension, see Creating Container Extensions. For information about third-party container extensions, see the Unity Community Site on CodePlex.


You can add a custom extension to the Unity container in one of two ways:

You can also access configuration information for an extension that exposes such information. For details, see Accessing Configuration Information for Extensions.

You can add a custom extension to the Unity container by compiling the extension and specifying the type and assembly name in the configuration file for the Unity Application Block. The following XML shows the outline of a suitable XML configuration file containing the <extensions> element where you add details of the extensions you want the container to load. You can also implement configuration classes for your custom extensions and specify these in the <extensionConfig> element.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>

<configSections>
  <section name="unity"
            type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration" />
</configSections>

<unity>

  <containers>

    <container name="containerOne">

      <types>
        ...
      </types>

      <instances>
        ...
      </instances>

      <extensions>
        <add type="MyApp.MyExtensions.SpecialOne" />
        <add type="MyApp.MyExtensions.Another", MyApp.MyExtensions,
                   Version=x.x.x.x, Culture=neutral, PublicKeyToken=..." />
      </extensions>

      <extensionConfig>
        <add type="MyApp.MyExtensions.SpecialOne.Config" />
        <add type="MyApp.MyExtensions.Another.Config", MyApp.MyExtensions,
                   Version=x.x.x.x, Culture=neutral, PublicKeyToken=..." />
      </extensionConfig>

    </container>

  </containers>

</unity>

</configuration>

By default, this file is the App.config or Web.config file for your application. However, you can use any other file or configuration source you require. You must write code in your application to load the configuration information and prepare the container. It does not do this automatically. For more information about configuration files, and loading configuration information, see Entering Configuration Information.

You can add a custom or third-party extension to the Unity container at run time using the AddExtension and AddNewExtension methods, and you can remove extensions using the RemoveAllExtensions method.

To add an extension, use the AddNewExtension or AddExtension method of the container. For example, this code creates a new instance of an extension class named MyCustomExtension that you have previously created, and which resides in this or a referenced project, and adds it to the container.

IUnityContainer container = new UnityContainer();
container.AddNewExtension<MyCustomExtension>();

If you already have an existing extension instance, you add it to the container using the AddExtension method. For example, this code adds an extension instance referenced by the variable named myExistingExtension to the container.

IUnityContainer container = new UnityContainer();
container.AddExtension(myExistingExtension);

You cannot remove individual extensions from the container. However, you can remove all currently registered extensions and then add back any you want to use. To remove all existing extensions, use the RemoveAllExtensions method of the container. For example, this code removes all extensions from an existing container referenced in the variable named container.

container.RemoveAllExtensions();

Container extensions will usually add strategies and policies to the container, as described in the topic Creating Container Extensions. However, extensions can expose configuration interfaces that allow them to read and expose configuration information. The extension must provide its own features to read and manage configuration information.

To access the configuration information for an extension, use the Configure method. This method of the UnityContainer class walks the list of extensions added to the container and returns the first one that implements the type you specify in the parameter of the Configure method. The method returns the required configuration interface, or it returns null (Nothing in Visual Basic) if the configuration type is not found.

There are two overrides of the Configure method. You can specify the extension type in the generic overload as a "configurator" (a class that implements the IUnityContainerExtensionConfigurator interface), or you can use the non-generic overload that accepts and returns an Object type. The following code shows both overrides of the Configure method used to retrieve the configuration as a type named MyConfigInterface.

// using a "configurator" where MyConfigInterface implements 
// the IUnityContainerExtensionConfigurator interface
MyConfigInterface configurator = container.Configure<MyConfigInterface>();

// using an Object type
Object config = container.Configure(typeof(MyConfigInterface));

For more information about configuration for container extensions, see Creating Container Extensions.


Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.