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?
According to Microsoft documentation AppDomain.CurrentDomain.GetAssemblies()
gets the assemblies that have been loaded into the execution context of this application domain. About AppDomain.CurrentDomain.GetAssemblies()
It seems that you need to change strategy of loading the assemblies you need from using the appdomain to looking for dlls in your applications folder.
I found a discussion on a similar problem here
You can handle the AssemblyResolve event and load AncillaryProject.dll
in that event handler
http://msdn.microsoft.com/en-us/library/ff527268.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With