Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referenced Assembly Not Found - How to get all DLLs included in solution

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?

like image 487
eouw0o83hf Avatar asked Apr 03 '12 16:04

eouw0o83hf


2 Answers

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

like image 81
Adil Avatar answered Sep 24 '22 20:09

Adil


You can handle the AssemblyResolve event and load AncillaryProject.dll in that event handler

http://msdn.microsoft.com/en-us/library/ff527268.aspx

like image 21
Eric J. Avatar answered Sep 26 '22 20:09

Eric J.