views:

305

answers:

2

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

Problem: since the .dll/.exe I loaded up with Reflection doesn't unload until the app domian unloads, it is now locked and the projects can't be built again because VS tries to re-create the files (all standard stuff). I have tried creating temporary files, then reflecting over them...no worky, still have locked original files (I totally don’t understand that BTW). Now I am now going down the path of creating a temporary AppDomain to load the files into and then destroy. I am having problems loading the files though:

The way I understand AddDomain.Load is that I should create and send a byte array of the assembly to it. I do that:

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 greatly appreciated. My head is lopsided from beating it against the wall...

Thanks, Dan

+1  A: 

What's almost certainly happening here is that one of the dependencies of WindowsFormApplication1 are not available in the constructed AppDomain and hence it's not able to load WindowsFormApplication1. In order to tell them you're going to have to check the fusion binding log to see what happened. Here is a great tutorial on how to debug assembly binding failures to detect their root cause.

JaredPar
Great suggestion, I should have done this before posting. Trying to get into FusLog right now and apparently I didn't load everything when I installed VS...Thanks!
Yep, that helped a lot: I clearly see that the temp domain's base directory (I guess) is set to "C:/Program Files/Microsoft Visual Studio 9.0/Common7/IDE". Obviously, that's why its not finding the file. Only, now...why didn't my AppDomainSetup.ApplicationBase take?!
A: 

I'm basically trying to do the same thing and having the same problem. Apparently this 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 (I can't post another hyperlink)

Mark Sowul