0

I want to map most of my interfaces to concrete classes in my app.config file. However, I would like to register some interfaces to the same Unity catalog at runtime. I tried the code below, but it gives me a SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code.

IUnityContainer container = new UnityContainer();

UnityConfigurationSection configSection =
    (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
configSection.Containers.Default.Configure(container);

container.RegisterInstance<IInterface>(new ConcreteObject());

How can I register an object at runtime in a Unity catalog initialized from app.config?

I am using the Unity version (2.0) that ships with Prism4.

shenku
  • 11,969
  • 12
  • 64
  • 118
MvdD
  • 22,082
  • 8
  • 65
  • 93

1 Answers1

0

This is a common problem, but not technically an error, with registering unity objects. Change your regsitration to this:

container.RegisterType<IInterface, ConcreteObject>(new ContainerControlledLifetimeManager());

That should fix your problem.

That exception gets thrown because of this:

Can Unity be made to not throw SynchronizationLockException all the time?

Community
  • 1
  • 1
poindexter12
  • 1,775
  • 1
  • 14
  • 20