Configuring 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. |
The latest Unity Application Block information can be found at the Unity Application Block site. |
You can create your own custom container extensions, or use container extensions created by third parties, with the Unity Application Block. Unity also uses default container extensions to implement its own functionality.
For information about using extensions see Using Container Extensions.
Specifying Extensions in the Application Configuration File
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.
Adding and Removing Extensions at Run Time
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>();
'Usage
Dim container As IUnityContainer = New UnityContainer()
container.AddNewExtension(Of 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);
'Usage
Dim container As IUnityContainer = 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();
'Usage
container.RemoveAllExtensions()