26

Several times a day I receive this error while running my asp.net mvc4 project. I am not asking what that means, or how to fix it, but what am I doing to cause it? It is really starting to bug me, and if I can cause it to go away by altering my workflow I will.

Cannot create/shadow copy '< assembly >' when that file already exists.

As I mentioned above the project is an MVC4 application. My workflow is typically.

  • Make a change (code or html)
  • If it was code, rebuild the project (using keyboard shortcut CTRL+SHIFT+B)
  • refresh the page
  • see annoying error
    • clean the solution
    • rebuild solution
    • refresh page
  • verify change
  • goto 10

(I get the error with/without the debugger attached.) I will be eternally grateful to anyone who can inform me on how to avoid this error, and will promptly tweet your praises

friism
  • 19,068
  • 5
  • 80
  • 116
  • Did you ever get to the bottom of this? Still having this issue in November '14 with Visual Studio 2013 Update 4! – JMK Nov 17 '14 at 13:58
  • 1
    @JMK As the accepted answer suggests, I just stopped and counted to 3 after the build was complete. It usually isn't a problem anymore because live reload takes care of it for me –  Nov 17 '14 at 17:08

3 Answers3

26

I find this annoying too. It seems to happen when you try to refresh the page in a browser before the build is fully complete. Try counting to three-one-thousand after build is complete and then refresh in the browser. Create/shadow copy should be done by that point.

I also don't think you should need to "clean solution". I believe a rebuild is a clean + build, so you may be cleaning twice.

After a bit of research, I also found this pre-build event which seems to be popular. (this workaround does not seem to work, perhaps it did in previous versions of VS / .NET framework)

Update: alternate solution

Another way to get by this is, when you see the YSOD, just type in a different URL in the browser and load it instead. Then click the back button and refresh to get to the page you were trying to reload in the first place.

Community
  • 1
  • 1
danludwig
  • 46,965
  • 25
  • 159
  • 237
  • Let me try that for a bit :) In regards to the clean/rebuild http://ux.stackexchange.com/questions/11660/why-do-people-clear-the-screen-multiple-times-when-using-a-calculator –  Apr 24 '13 at 17:01
  • 1
    I get this shadow copy exception often when I'm working fast, and I never clean. Rebuild and pause solves it 100% if the time for me. – danludwig Apr 24 '13 at 17:03
  • 1
    I have an SSD installed, so I doubt it is latency. –  Apr 24 '13 at 17:19
  • 1
    I saw a JumpStart with Scott H. and Jonh G., they are leaders of ASP.NET, and they had this problem live. Then, they said something about VS 2012 Bug that they were trying to solve soon. – Fals Apr 24 '13 at 18:09
  • I can't dig up your twitter profile, so I will just say thanks here. :) –  Apr 25 '13 at 13:33
  • This problem just arised today after having worked on a project for more than two years. What I have changed? I have today created a web.Test.Config transform file. Guess that is causing the problem. Would be nice to know if that is the same case for all of the above users! – olf Aug 27 '14 at 11:56
7

What's causing this error seems to be doing the shadow copy (stating the obvious...), ie generating a file in one folder and then reading it to copy it elsewhere. I think what's causing the issue is generating or not that first file, and when/how it's copied, regardless of what your actions are. Does this first file (that will be shadow copied) needs regenerating, and therefore re-shadow copied?

This post says:

By default shadow copy is enabled on every appdomain created by ASP.NET. Assemblies loaded will be copied to a shadow copy cache directory, and will be used from there. So that the original file is not locked and can be modified.

And their fix is to prevent shadow copy. But I don't personnally fully understand what shadow copy does in the asp.net context, maybe someone can clarify if there are any bad side effect to disabling it:

<configuration>
    <system.web>
        <hostingEnvironment shadowCopyBinAssemblies="false" />
    </system.web>
</configuration>

This post talks about disabling indexing and/or antivirus, which would suggest that building the project generates new files, and the antivirus (or indexing service) accesses them and locks them, just when the shadow copy is trying to copy that exact same file.

I get his error regularly and never found what specific action I do causes the error. I make a code change, press F5 (Run/ Play button), get the error in the web browser, close the web browser, press F5 again. It now works. I don't clean, I don't rebuild. However, with the second F5/Run, I believe VS detects there's been no code change, and therefore, maybe the shadow copy is not redone? Or maybe this time the shadow copy is timed slightly differently and it works.

Community
  • 1
  • 1
Thierry_S
  • 1,526
  • 16
  • 25
  • 2
    The problem with disabling shadow copy is that VS would not be able to replace the assemblies on the next build; instead the assembly files would be in use by the web application. Shadow copy creates a copy of the assemblies to be loaded instead, allowing for the original assemblies to be overwritten at anytime. – Michael Petito Oct 08 '13 at 13:47
1

I received this error after adding a new image to my web project.

In VS, each image has a property entitled "Copy to Output Directory". I chose "Copy Always", then I got the message.

I changed it to "Do Not Copy" - now it is happy.

Hope that helps someone.

Michael
  • 173
  • 1
  • 3