I'm running a WCF application CoreApplication whose VS project has a reference to AncillaryProject. CoreApplication uses a class Provider from AncillaryProject; however, it is never explicitly referenced - it's invoked via Reflection.
My problem is that sometimes CoreApplication fails to find Provider because AncillaryProject does not come up in the call to GetAssemblies(). Sometimes it works fine, but sometimes (I'm guessing it may be after a JIT) it fails.
Here's my original code:
var providers = from d in AppDomain.CurrentDomain.GetAssemblies()
                from c in d.GetTypes()
                where typeof(BaseProvider).IsAssignableFrom(c)
                select c;
After looking at this question, I tried using GetReferencedAssemblies():
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
    allAssemblies = allAssemblies.Union(
                          a.GetReferencedAssemblies()
                           .Select(b => System.Reflection.Assembly.Load(b)));
}
var providers = from d in allAssemblies
                from c in d.GetTypes()
                where typeof(BaseProvider).IsAssignableFrom(c)
                select c;
I realize that the question I referenced solves the problem through dynamically loading all dll files in the bin directory, but that doesn't sound particularly good to me. Is there a better way to do this, or is .NET simply not loading the other Assemblies in at all? How does this work under the hood, and is there anything I can do about it?