Redirecting Unity Xml Configuration

by Administrator 4. Dezember 2010 03:31

One of the nuisances with unity is, that is expects it's Xml configuration to be physically present in the app.config/web.config. I saw a post on the discussion forums, where someone asked, whether it's planned to implement a hook allowing to provide other sources and the clear answer was no. The standard approach they take has some drawbacks:

  • Your App.config/Web.config get's seriously cluttered up when having hundreds or thousands of registrations.
    Since there is also no way of doing includes, this will turn into a pure nightmare, especially considering that there might be other vital configurations in the file (e.g. the connection string).
  • App.config/Web.config are prone to contain installation specific data (e.g. connection strings). If you deliver an update you probably have to include the file to update the IoC settings and need to deal with preserving the other configuration settings.
  • There is no real way of dealing with scenarios, where the configuration gets dynamically compiled (this is the reason why I looked into this).
    Let's assume, you have configurations you need to merge, e.g. you have a standard configuration and can provide overrides specific to a customer installation. In that case you could use some sort Xsl transform of handcoded approach to generate the real configuration from different sources. This also gives you a good way of integrating different configuration files making development much easier.

The only thing I found that pointed into the direction of using another file is, to change the name/location of the configuration file that the configuration manager will use to get Unity's configuration. Take a look at this post to see how it's done. Though this works and solves problem 1 & 2, problem 3 is still present.

After some tweaking I found out, that the solution is easier than expected. Btw. this should work for any configuration you want to get out of the app.config/web.config.

First I created a class that inherits from Unity's configuration section.

using System.Xml;
using Microsoft.Practices.Unity.Configuration;

namespace UnityExtension
    /// <summary>
    /// Provide a way to read configuration sections from an xml reader rather than
    /// from a physical app.config/web.config file.
    /// </summary>
    public class StreamSourcedUnityConfigurationSection : UnityConfigurationSection
        /// <summary>
        /// Deserialize from a given xml source.
        /// </summary>
        /// <param name="reader">Xml source.</param>
        public void DeserializeFromXml(XmlReader reader)
            // Call into base deserialisation mechanism.

The only thing that this inherited class provides for us, is to expose the desirialisation to a public method. The reader argument can source it's content in a physical file or somewhere else (e.g. a memory stream).

To consume the configuration I used the following code to set up the container:

IUnityContainer container = new UnityContainer();
XmlTextReader reader = new XmlTextReader("unity.config");
StreamSourcedUnityConfigurationSection sec = new StreamSourcedUnityConfigurationSection();

One important thing to know, is that the config file you use has to directly start with the <unity> tag. Even an <?xml> descriptor will raise an error. Here's the beginning of the unity.config file:

<unity xmlns="">
        type="UnityExtension.ITestClass, UnityExtension"
        mapTo="UnityExtension.TestClass, UnityExtension">

I hope you'll find this somehow helpful in your projects. Feel free to give me some feedback.

Tags: ,

My Stuff | Unity

Autowireup Extension for Unity

by Administrator 2. Dezember 2010 14:36

I added an autowireup extension for Unity that allows using autowire by name/by type. Check this page for more information.

Tags: , ,

My Stuff | Projects | Unity

About the author

for comments and suggestions contact:


Month List