Is it possible to change the registered type at runtime?

Apr 26, 2012 at 4:33 AM
Edited Apr 26, 2012 at 4:35 AM

I am currently using Unity with MOQ to do my unit testing for WCF. In the application's code, I have the following:


private void MyMethod()
    _proxy = new UnityContainer().LoadConfiguration().Resolve<IMyInterface>();

In the application's app.config, I have the following:

  <register type="IMyInterface" mapTo="MyActualObject" />

In the unit test's app.config, I replace that with my mock object implementation of the proxy.

  <register type="IMyInterface" mapTo="MyMockObject" />

That is all working fine. But what I would like to do further is, for certain tests, I would like to replace MyMockObject with a different mock object implementation.

Is it possible to change the registered type at runtime? I have tried doing the following in my unit test but fails cause the configuration file is readonly.

((UnityConfigurationSection)ConfigurationManager.GetSection("unity")).Containers[0].Registrations[0].MapToName = "AnotherMockObject"


Apr 26, 2012 at 4:59 AM

As you mention, the configuration is readonly.  It is usually possible to read in the configuration and then create an entire new configuration but that is tedious.

Do you have to use a configuration file for this?  Can you create the configuration programmatically?  It is very easy to overwrite existing configuration using the programmatic configuration.  E.g.:

    IUnityContainer container = new UnityContainer().LoadConfiguration();

    // Returns previously registered MyLogger from config
    var logger = container.Resolve<ILogger>();

    // Overwrite ILogger registration with new type registration
    container.RegisterType<ILogger, AnotherLogger>();

    // Now returns AnotherLogger instead of MyLogger
    logger = container.Resolve<ILogger>();

Randy Levy
Enterprise Library support engineer 

Apr 26, 2012 at 5:09 AM
Edited Apr 26, 2012 at 5:10 AM

Hi Randy,

I have managed to modify the configuration file at runtime but Unity does not seem to detect the change.

Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 var section = (UnityConfigurationSection)appConfig.GetSection("unity");
 section.Containers[0].Registrations[0].MapToName = "AnotherMockObject";  

It is still loading "MyMockObject" instead of "AnotherMockObject"


Any ideas?


Apr 26, 2012 at 5:16 AM

The configuration is in the app.config. Unfortunately, I do not think I can use programmatic configuration since the application code is not aware about the unit testing code which is why I am using the app.config files to define which instance to load (Unit testing object or the actual implementation).

I think it would be weird to reference the unit testing object/implementation in the actual application code.

Apr 26, 2012 at 5:45 AM

Looks like it was not working because it was being cached. I had to do


to get it to work.

Is changing the configuration file the only way to achieve what I need?

Apr 27, 2012 at 5:05 AM
Edited May 4, 2012 at 2:10 AM

> Is changing the configuration file the only way to achieve what I need?

I think the answer, given the posted approach, is yes.  

It looks like the MyMethod method is dependent on the Unity configuration file (to retrieve the proxy class).  The prevailing approach I've seen is that the proxy class would be injected into MyMethod containing class.  That makes it easy to inject (and switch concrete) dependencies.

Also, as a suggestion it's probably not the most efficient approach to create a UnityContainer and load the configuration every time a proxy class is required.

Randy Levy
Enterprise Library support engineer