简体   繁体   中英

How can I extend a Unity catalog at runtime?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM