0

I try to create a modulable system in which each module can depend on other ones. Each module is a NuGet and their dependencies have to be resolved via NuGet. Each module contains a Type which will implements an shared Interface which allow for extracting a list of instruction.

All these instructions are to be executed in reverse order.

The detailed situation is this:

  1. I have 4 projects which I will call API, S, M1, M2. They are all NuGets and they depends one from another is that way (-> stands for 'depends on'): S -> API, M1 -> S, M2 -> M1.

  2. On the other hand we have the projet CORE, CONSOLE and TESTS. They aren't NuGets but they references each others in this way (-> stands for 'references'): CORE -> S, CONSOLE -> CORE, TESTS -> CORE.

  3. M1 et M2 are modules, once again they are special because they contains a Type which must be found and instantiated from within CORE.

The goal is this:

  1. I must fetch and install locally NuGets from a given repository.

    • This is done, it fetch the NuGets, unzip them into a given location.
  2. I must inject the dlls from this extraction into the installer as if they were some sort of plugins.

    • How do I inject the correct dlls from the imported NuGets knowing that:
      1. some of them are already present in the project CORE (notably the dlls that are present in the projects API and S).
      2. some of them contain copies of the dll for different .NET Frameworks.
      3. when I succeed to inject the dlls (by some artificial processes of filtering the lib/whateverframework directories and I try to find all the types which have a specific Attribute (which is located in the shared project API), this attribute seems to be different if I use the one which is in the dlls from NuGet and the one which I can directly use in VS.

I use C# 6 and VS2015.

How could I load dlls into the running application with all its dependencies using NuGet core API to solve dependencies? What is the best/proper way to do this?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Ephasme
  • 286
  • 1
  • 10
  • This is a bit broad... Do you have a more specific question that just generally "how do I do this?" – Robert Harvey Jun 18 '15 at 22:20
  • I edited my post to ask a more specific question... I know this question has been asked many times but none of the answers happened to be a good solution to me... – Ephasme Jun 19 '15 at 07:26
  • You should look at MEF for injecting the DLL's or types. I'm not sure NuGet is the correct tool for this; it kinda depends on what you want to do. NuGet is oriented for public consumption, so if you're not distributing the DLLs to the public, it's a bit overkill. – Robert Harvey Jun 19 '15 at 14:13
  • See also http://stackoverflow.com/q/1137781/102937 – Robert Harvey Jun 19 '15 at 14:13
  • I will actually distribute the modules to the public eventually... that's the reason why I need NuGet. And plus I need to resolve dependency so I want to use NuGet to do that. But except from this I would like to use some tool to take the dlls I imported with NuGet and inject them into the current application... which will go through the types and fetch the ones which have a specific attribute. – Ephasme Jun 19 '15 at 14:56
  • See MEF and the SO post I linked. You might not need MEF; you might just need `Activator.CreateInstance()`. – Robert Harvey Jun 19 '15 at 14:59
  • Thank you anyway! I'll keep you posted. – Ephasme Jun 19 '15 at 15:03

1 Answers1

1

A sample code you could use:

var localRepository = new SharedPackageRepository(packagesPath);
var allAssemblies = localRepository
            .GetPackages()
            .ToArray()
            .SelectMany(p => p.AssemblyReferences.Select(a =>
            {
                var path = Path.Combine(packagesPath, p.Id + "." + p.Version, a.Path);
                var aname = AssemblyName.GetAssemblyName(path);
                return new { key = aname.FullName, value = path };
            }))
            .DistinctBy(i => i.key)
            .ToDictionary(i => i.key, i => i.value);
AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
        {
            var aname = new AssemblyName(eventArgs.Name);
            if (allAssemblies.ContainsKey(aname.FullName))
            {
                return Assembly.LoadFile(allAssemblies[aname.FullName]);
            }
            return null;
        };
tenkod
  • 297
  • 1
  • 4
  • 9