Reputation: 929
I have a Unity application that is running and working perfectly but we are currently in the process of cleaning up our code by acting on all the compilation warnings.
I get a warning on the following piece of code because of the obsolete Microsoft.Practices.Unity.Configuration.ContainerElement.Configure method:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
var container = new UnityContainer();
foreach (ContainerElement containerElement in section.Containers)
{
containerElement.Configure(container);
}
Container = container; // Set the main container
}
I'd like to replace it with the UnityConfigurationSection.Configure method as suggested but can't see that they are equivalent because of being at different levels of the object hierarchy.
I've tried:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
IUnityContainer container = new UnityContainer();
container = section.Configure(container);
Container = container; // Set the main container
}
but that falls down with null references.
How should I be updating the code to eliminate use of the obsolete method?
Upvotes: 3
Views: 3694
Reputation: 11
If the section in the config doesn't have a name, it takes it as default and you don't need to specify a name:
<containers>
<container>
<types>
<type type="IObject" mapTo="Object" />
</types>
</container>
</containers>
Upvotes: 1
Reputation: 929
I have managed to get this to work. The secret was using the overload of the Configure method that takes a container name to configure each container element:
foreach (ContainerElement containerElement in section.Containers)
{
container = section.Configure(container, containerElement.Name);
}
So far this looks as if it's doing the same as the obsolete Container.Configure method was doing - everything is working as expected.
Upvotes: 1