Loading .dll / .exe from file into temporary AppDomain instance throws exception

I'm trying to make a Visual Studio AddIn that removes unused references from projects in the current solution (I know it can be done, Resharper does it, but my client doesn't want to pay for 300 licenses). Anyhoo, I use DTE to project projects, compile their assemblies, and then ponder those assemblies to get their referenced assemblies and cross-examine the .csproj file.

Problem: Since the .dll / .exe loaded from Reflection doesn't unload until the domian app is unloaded, it is now locked and projects cannot be built again because VS is trying to re-create the files (all standard stuff). I tried creating temporary files and then pondering over them ... didn't work, still locked the original files (I don't fully understand this BTW). Now I am now going down the path of creating a temporary AppDomain for file upload and destruction. I am having problems uploading files:

As I understand it, AddDomain.Load is what I have to create and send the assembly byte array. I'm doing it:

FileStream fs = new FileStream(assemblyFile, FileMode.Open);
byte[] assemblyFileBuffer = new byte[(int)fs.Length];
fs.Read(assemblyFileBuffer, 0, assemblyFileBuffer.Length);
fs.Close();

AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = assemblyFileInfo.Directory.FullName;
AppDomain tempAppDomain = AppDomain.CreateDomain("TempAppDomain", null, domainSetup);

Assembly projectAssembly = tempAppDomain.Load(assemblyFileBuffer);

      

The last line throws an exception:

"Could not load file or assembly" WindowsFormsApplication1, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null "or one of its dependencies. The system cannot find the file specified.": "WindowsFormsApplication3, Version = 1.0. 0.0, Culture = neutral, PublicKeyToken = null "}"

Any help or thoughts would be much appreciated. My head is bent from hitting the wall ...

Thanks, Dan

+2


source to share


2 answers


What is almost certainly happening here is that one of the dependencies of WindowsFormApplication1 is not available in the built AppDomain and therefore cannot load WindowsFormApplication1. To tell them you will need to check the fusion binding log to see what happened. Here's a great tutorial on how to debug assembly binding failures to find the root cause.



+1


source


I am basically trying to do the same and have the same problem. Apparently it won't work.

http://blogs.msdn.com/brada/archive/2003/04/16/49974.aspx



I ended up using method 3, although it's ugly: See KB 837908

0


source







All Articles