3

I have a multiple projects that are loading assemblies through unity IoC. There is an xml file that contains the configuration of the container like

<component
    id="MyComponent"
    service="MyNamespace.MyInterfaces.IMyComponent, MyNamespace.MyInterfaces"
    type="MyNamespace.MyClasses.MyComponent, MyNamespace.MyClasses"
    lifestyle="singleton">
  <parameters>
    <systemId>ID</systemId>
    <!-- ctor -->
  </parameters>
</component>

All of these projects have their own xml configuration file but share the same assemblies. What I would like to do is to place all the common assemblies in one folder (in the intranet) and let the unity container dynamically loads the assemblies from there. This for maintainability purpose: in this way when I have to update the assemblies I have all of them in one place only.

What I've tried so far:

  1. Googled until I got an headache
  2. Intercept the AssemblyResoveEvent to manually load assemblies (as suggested in this answer) - with no luck

    protected override void OnStartup(StartupEventArgs e)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
      ...
    }
    
    static Assembly LoadFromSameFolder (object sender, ResolveEventArgs args)
    {
        string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        string assemblyPath = Path.Combine(folderPath, "DLL\\", new AssemblyName(args.Name).Name + ".dll");
        System.Diagnostics.Debug.WriteLine("ASS: " + assemblyPath);
        if (!File.Exists(assemblyPath)) return null;
        Assembly assembly = Assembly.LoadFrom(assemblyPath);
        return assembly;
    }
    
  3. Struggled with PrivateBinPath property but there you can specify only sub-folders - not my case

Any suggestions? Is this even possible?

Community
  • 1
  • 1
garlix
  • 576
  • 9
  • 26
  • "and let the unity container dynamically loads the assemblies from there.". This will be horror unless you make a local copy first, because once an assembly is loaded, the file is locked until _all_ applications are closed. Otherwise you'll find yourself in a situation where you'll be unable to update the assemblies and have to ask request all users to close their application (which you can't because of that single user that just locked his computer and went on holliday). – Steven Mar 10 '17 at 18:36
  • Thank you for the comment but the way I'm going to update the assemblies is not the relevant part of my question. The software is not intended to be used from any users. I have total control of the PCs where there is the program that loads the assemblies with unity. Indeed, if no one answers my question I will create an upgrade script to copy new versions of the assemblies on all the PCs. – garlix Mar 11 '17 at 22:46

0 Answers0