views:

1199

answers:

5

Hi,

I am working on a solution consisting of 8 .NET projects. Since I am practicing TDD, I have to re-compile my solution very often. Lately I have been getting the following error about every second time when trying to compile:

Error 2 Unable to copy file "obj\Debug\Zeiterfassung.Tests.dll" to "bin\Debug\Zeiterfassung.Tests.dll". The process cannot access the file 'bin\Debug\Zeiterfassung.Tests.dll' because it is being used by another process.

Zeiterfassung.Tests.dll is the dll generated by one of my projects (it's the unit testing project). It's always this dll that cannot be copied and causes the error. Everything else works fine 100% of the time.

In about 9/10 times I can "solve" the problem by recompiling my solution again. But when the problem is getting really bad, the project just won't compile successfully no matter how often I try and I have to restart the IDE.

I used microsoft's handle.exe to ascertain which process is locking the DLL and it is devenv.exe. I also tried deleting the DLL by hand and it really can't be deleted until I restart the IDE.

Last but not least, I tried adding <GenerateResourceNeverLockTypeAssemblies>true</GenerateResourceNeverLockTypeAssemblies> to my project as suggested in another forum, but this did not help.

Please help! This problem is really starting to drive me nuts.

Edit: I might also add that I made sure my unit tests are finished when this problem occurrs. Still, the dll remains locked. I am running my tests via the Resharper unit test explorer.

+3  A: 

I've faced the same problem before. Process Explorer is able to delete the handle.

Mark P Neyer
You're right! But how can I automate this? The problem occurs so often that closing the handle in ProcessExplorer is way too much overhead.
Adrian Grigore
Two things. The Process Explorer should have indicated what process had the file open. Was it a debug-run of the process with a loose thread? Visual Studio itself? Second, the only time my team has a problem with the "DLL locking" thing is when we've got a circular reference in the project. Project A requires B requires C which requires A. Visual Studio itself is usually the one with the DLL locked.
clintp
Visual Studio itself has the DLL locked. But I could not find any circular reference.
Adrian Grigore
+1  A: 

The most likely problem is a threading issue. You probably had an itinerant thread that is still executing, and it has the reference to the .DLL.

Randolpho
Thanks for the hint, but how can I fix the problem?
Adrian Grigore
Adrian, does your project use mulithreading capabilities of some kind? The issue is probably in your application's code somewhere.
Spencer Ruport
@Adrian: As Spencer suggests, if you're using threads the fix would be in your code. There are too many possibilities to suggest a fix at this point.
Randolpho
No, I am not using any explicit multithreading in my project. My solution consists of one ASP.NET MVC project, one unit testing project and lots of class libraries. None of my code explicitly deals with multithreading.
Adrian Grigore
A: 

Visual Studio has had one or another problem like this since Day 1. It would be nice to give you a set of simple solutions that always work, but frankly, sometimes, it has just "tripped over its own feet".

In that case, the simple solution is to exit and restart. Even closing the solution and reopening may not help.

John Saunders
Yes, this is what I have been doing so far. But having to restart the IDE every 10 minutes seriously impacts my productivity.
Adrian Grigore
A: 

By "I tried adding true to my project as suggested in another forum" do you mean that you created a property named GenerateResourceNeverLockTypeAssemblies and set it to true as suggested at http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/6f76db9a-ea37-42b3-a016-571912c28032? If not try that out.

Sayed Ibrahim Hashimi
Oops, forgot to mark the GenerateResourceNeverLockTypeAssemblies tag as code and it was therefore not shown as such in my posting. Yes, that's what I already tried.
Adrian Grigore
A: 

Looks like the bug disappeared (fingers crossed...) after I moved my test project, checked out an earlier version of the project from the repository and replaced all code files with the newer versions.

Adrian Grigore
If this worked, it worries me. It means you have an issue in your code somewhere that you have accidentally added and have now removed. It could lead to instability later.
Randolpho
It rather seems to me like I had introduced an issue which led to this problem sometime within the last few days and now I removed the issue with my code by reverting to the previous code version.
Adrian Grigore
Maybe you just needed to reboot, you are using Windows lol.
Sayed Ibrahim Hashimi