1

I have a Solution with 4 Projects and these projects make references each other in this way:

  • Project RoleCreator makes reference to Project Unify.ServiceInterface and Unify.ServiceModel

I want to pre load the assemblies from ALL THE PROJECTS in the solution at some method in the Project RoleCreator, but in this line Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); in the breakpoint value appears load just the Assembly from the projectUnify.ServiceInterface but not the Assembly from the project Unify.ServiceModel that it's also referenced in the project RoleCreator.I want to load all the assemblies from THE SOLUTION!

enter image description here

Alyafey
  • 1,455
  • 3
  • 15
  • 23
rr7
  • 163
  • 2
  • 12
  • This should give you a starting point: http://stackoverflow.com/questions/3814141/search-for-interface-in-all-assemblies-of-bin – Cosmin Vană Jan 13 '14 at 23:02
  • check out this....... http://stackoverflow.com/questions/3021613/how-to-pre-load-all-deployed-assemblies-for-an-appdomain?rq=1 – Coderz Jan 14 '14 at 04:22

2 Answers2

1

You can use such code:

public static void PreloadAssembly(Assembly assembly)
{
    var references = assembly.GetReferencedAssemblies();
    foreach (var assemblyName in references)
    {
        Assembly.Load(assemblyName);
    }
}

And then

PreloadAssembly(typeof(TypeFromAssembly1).Assembly);
PreloadAssembly(typeof(TypeFromAssembly2).Assembly);

This call

AppDomain.CurrentDomain.GetAssemblies()

Will return list of already loaded assemblies. If you didn't access type from some assembly, then AppDomain won't load it, and it won't be present in GetAssemblies call. AppDomain loads assemblies for types that you used.

And as an another way to do it is described here - how to load all assemblies from within your /bin directory . It just loads all assemblies from bin folder

Community
  • 1
  • 1
Sergey Litvinov
  • 7,408
  • 5
  • 46
  • 67
0

If it is web application, you can use BuildManager.GetReferencedAssemblies. Here is an example from Autofac wiki (while it is not relevant that it comes from Autofac):

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

That will force the referenced assemblies to be loaded into the AppDomain immediately making them available for module scanning.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58