Multithreading issues with Unity

Jul 11, 2012 at 2:21 PM

I have read that RegisterType is not thread safe in Unity. (http://unity.codeplex.com/discussions/234779)

I thought that TecX might help solve that, do you have any ideas on how this can be solved?

Nick

Coordinator
Jul 11, 2012 at 2:50 PM

Short answer: Nope. Nor do I see why it should be solved at all.

Setting up your container should be done in exactly one location and exactly once. There is no need to parallelize the bootstrapping code.

Jul 11, 2012 at 4:25 PM

How would you suggest doing this in MVC? I have done it like this...

Global.asax

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            var container = Bootstrapper.Initialise();

BootStrapper.cs

    public class Bootstrapper
    {
        public static IUnityContainer Initialise()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));

            return container;
        }

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();
            
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.Scan(s =>
                             {
                                 s.RegisterConcreteTypesAgainstTheFirstInterface();
                                 s.AssembliesFromApplicationBaseDirectory();
                                 s.Exclude(o => !typeof(IDependency).IsAssignableFrom(o));
                             });
            builder.Extension<CollectionResolutionExtension>();
There is a lot more setup in the bootstrapper around this.

Whatcha think? I think the problem is due to it being a static class, not sure... Ideas?

Nick

Jul 12, 2012 at 10:09 AM
Hi Again, Sorry to keep bothering you but I how do I specify a Lifetime manager when registering within Scan?

ConfigurationBuilder builder = new ConfigurationBuilder(); builder.Scan(s => { s.RegisterConcreteTypesAgainstTheFirstInterface(); s.AssembliesFromApplicationBaseDirectory(); s.Exclude(o => !typeof(IDependency).IsAssignableFrom(o)); });

Cheers, Nick
Coordinator
Jul 12, 2012 at 11:55 AM

Hi,

I'm not an expert on ASP.NET MVC but your solution looks OK from my perspective.

@Lifetime: You can't. If you want to assign special lifetimes (other then TransientLifetimeManager) you should consider writing a custom convention that gives you that ability.

Jul 12, 2012 at 2:27 PM

Okay so I have found the threading issues.... they are in this file...

http://unitymvc3.codeplex.com/SourceControl/changeset/view/580f45a682a5#Unity.Mvc3%2fUnityDependencyResolver.cs

around this line "isRegistered = ChildContainer.IsRegistered(typeToCheck);" near the bottom.... 

Its a shame your solution does not have a UnityDependencyResolver, do you have one on the roadmap?
Coordinator
Jul 12, 2012 at 8:09 PM

My next project will most likely be a web project. Maybe I will give it a try then. But that project is not due till end of september.