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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With