Unit test error in TFS build machine due to AppDomain.CurrentDomain

I had a problem with my unit tests that do not run on my TFS Build machine, although they work on my developer's machine.

I get an exception in the following line when trying to load all my loaded assemblies for a specific interface:

var classesToMap = AppDomain.CurrentDomain.GetAssemblies().ToList() .SelectMany(s => s.GetTypes()) .Where(p => typeof(IInterface).IsAssignableFrom(p) && p.IsClass).ToList(); 

The exception is:

System.Reflection.TargetInvocationException: exception thrown on the target of the call. ---> System.Reflection.ReflectionTypeLoadException: One or more of the requested types cannot be loaded. Get the LoaderExceptions property for more information ..

Any idea why?

+4
source share
3 answers

This is obtained by adding a condition to drop all the DLLs that are in the GAC:

  var classesToMap = AppDomain.CurrentDomain.GetAssemblies() .Where(a => !a.GlobalAssemblyCache) .SelectMany(s => s.GetTypes()) .Where(p => typeof(IInterface).IsAssignableFrom(p) && p.IsClass).ToList(); 

This is work, but it works for me.

Thanks for the tip.

+2
source

In my case, the only assembly that did not load was Microsoft.VisualStudio.TeamSystem.Licensing , which is a dependency on Microsoft.VisualStudio.QualityTools.Common .

Based on Osel Miko Dล™evorubec's answer , I came up with this safer strategy:

 private static Type[] GetTypesFromAssemblySafe(Assembly assembly) { var ignoredAssemblies = new List<string>() { "Microsoft.VisualStudio.QualityTools.Common" //Gives trouble in TFS automated builds }; if (!ignoredAssemblies.Contains(assembly.FullName.Split(',')[0])) return assembly.GetTypes(); else return new Type[] { }; } var types = AppDomain.CurrentDomain.GetAssemblies() .ToList() .SelectMany(GetTypesFromAssemblySafe); 
+1
source

I also met this problem, but the @nirpi did not condition helped me, I used this solution successfully:

 private static Type[] GetTypesFromAssemblySafe(Assembly assembly) { try { return assembly.GetTypes(); } catch { return new Type[] {}; } } var types = AppDomain.CurrentDomain.GetAssemblies() .ToList() .SelectMany(GetTypesFromAssemblySafe); 

source: http://www.kspace.pt/posts/dynamic-type-discovery/

0
source

All Articles