5

We have a windows service which is using Autofac, when we try to load the referenced assemblies not all are listed as some contain objects we aren't using anywhere in the application but interface implementations are in there we need to be included. The following method loads the assemblies:

private IEnumerable<Assembly> GetReferencedAssemblies(Assembly assembly)
{
  var assemblyNames = assembly.GetReferencedAssemblies();

  List<Assembly> assemblies = new List<Assembly>();
  assemblies.Add(assembly);
  foreach (var item in assemblyNames)
  {
    var loadedAssembly = System.Reflection.Assembly.Load(item.FullName);
    assemblies.Add(loadedAssembly);
  }

  return assemblies;
}

If we make a dummy reference to an object contained in the assembly then it loads the assembly and the types are built by autofac, if we remove the dummy object the assembly is no longer included.

Is there any way to include all referenced assemblies regardless of whether you are directly using an object in there (bearing in mind we still need it as the interface implementations are in there).

This works fine on ASP.NET as it just loads all DLLs in the bin.

user351711
  • 3,171
  • 5
  • 39
  • 74
  • 1
    There are at least a couple ways you could handle this. It sounds like a situation I would use MEF for. Put an [Autofac Module](https://code.google.com/p/autofac/wiki/StructuringWithModules) in every assembly and use MEF to get all the Modules, then Autofac can take over. – default.kramer Apr 03 '13 at 18:30

1 Answers1

1

If you do not actually reference a type in the assembly the compiler will remove the reference as it is assumed to be redundant. You need to manually load the required assemblies into the AppDomain using Assembly.Load(). How you determine the assemblies to load is up to you. You might choose to look through the files in a particular folder or you perhaps use a configuration file that contains the assemblies names.

Alex Meyer-Gleaves
  • 3,821
  • 21
  • 11