2

I need someone to explain to me what exactly the AssembliesInBaseDirectory method returns. I have inherited an application that has the following code:

namespace Infrastructure.Common {

    public static class GlobalContainer
    {
        private static IUnityContainer _unityContainer;

        public static IUnityContainer CreateUnityContainer()
        {
            var unityContainer = new UnityContainer();

            unityContainer.Configure(unityRegistry => unityRegistry.AddRegistry<CommonRegistry>());

            return unityContainer;
        }

        public static IUnityContainer Unity
        {
            get { return _unityContainer ?? (_unityContainer = CreateUnityContainer()); }
            set { _unityContainer = value; } // For testability
        }
    }
}

This GlobalContainer registers another registry called CommonRegistry and this is the code for that:

namespace Infrastructure.Common
{
    public class CommonRegistry : IocRegistry
    {
        public CommonRegistry()
        {
            const StringComparison strCmp = StringComparison.InvariantCultureIgnoreCase;

            Scan(scan =>v{  
                scan.AssembliesInBaseDirectory(a => a.GetName().Name.StartsWith(@"Pharmacy.", strCmp)
                    || a.GetName().Name.StartsWith(@"PharmacyServer.", strCmp)
                    || a.GetName().Name.StartsWith(@"CDM.", strCmp)
                    || a.GetName().Name.StartsWith(@"Commerce.", strCmp)
                    || a.GetName().Name.StartsWith(@"Infrastructure.", strCmp)
                    || a.GetName().Name.StartsWith(@"Audit.", strCmp)
                    || a.GetName().Name.StartsWith(@"Authentication.", strCmp)
                    || a.GetName().Name.StartsWith(@"LaSuite", strCmp));
                scan.ForRegistries(); 
            });
        }

        protected override void Register() 
        {
            RegisterWithConstructor<IUserContextProvider, UserContextProvider>(new IocInjectionConstructor());
            RegisterWithConstructor<ITenantProvider, TenantProvider>(new IocInjectionConstructor(new IocResolver<IUserContextProvider>()));
            Register<IImageProcessor, ImageProcessor>();
            Register<IValidationHelper, ValidationHelper>();
            Register<IClaimingHelper, ClaimingHelper>();
            Register<ICertificateHelper, CertificateHelper>();
        }
    }
}

I'm trying to understand the call to scan.AssembliesInBaseDirectory. You see, my solution has many many projects that use GlobalContainer. Some are WCF services, some are console applications. I want to know, when this method is called from the WCF service, what assemblies are being scanned? Where is it looking? When it is used in the console application, where is it looking? It seems to me that the base directory changes depending on who is using GlobalContainer. Is it looking for the assemblies that start with the given strings inside each of these projects (WCF and console)? I'm new to Ioc.

Steven
  • 166,672
  • 24
  • 332
  • 435
Ray
  • 4,679
  • 10
  • 46
  • 92

1 Answers1

0

Looks like you are using the Unity Configuration nuget package.

If you look at the source code in github, you will see the following:

public void AssembliesInBaseDirectory(Predicate<Assembly> predicate)
{
    AssembliesInDirectory(AppDomain.CurrentDomain.BaseDirectory, predicate);
    AssembliesInDirectory(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, predicate);
}

This means the code is looking at the following directories:

  • AppDomain.CurrentDomain.BaseDirectory, this is the directory from where the current application domain was loaded (like the exe directory or the root folder in ASP.Net). See this

  • AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, this is the list of directories under the application base directory that are probed for private assemblies. See this

And for all assemblies found in those directories is filtering all of those that don't start with the given strings. So those that start with the given strings will be scanned for types inheriting from UnityRegistry as per the ScanForRegistriesConvention. These registries will have a constructor where types will be registered in Unity.

Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112