Friday, June 7, 2013

Visual Studio 2012 Freezes or Crashes When trying to add a new ASP.NET MVC Controller

Occasionally while you are working on an ASP.NET MVC Project when you right-click on the Controller folder and choose "Add > Controller", you may notice that Visual Studio 2012 is hanging indefinitely or freezes up to the point you have to kill the "devenv.exe" Process through the Task Manager, only to find out that no matter how many times you repeat this cycle you are still not able to add Controllers in this manner. What makes it even more puzzling is that you can easily do the same with other ASP.NET MVC Project without any issues!

This issue has been reported to Microsoft over and over without them offering much of a solution:

http://connect.microsoft.com/VisualStudio/feedback/details/717662/visual-studio-consistently-freezes-after-opening-solution-and-after-debugging

http://connect.microsoft.com/VisualStudio/feedback/details/763452/vs2012-hangs-on-add-controller-when-project-originated-from-vs2010-express

Some workarounds suggested by various Developers are as follows:

  • You can also create a new solution in VS2012, then import all of the existing code files from the original solution. Add => Controller... will then start functioning properly again.
  • Select Add => New Item from the context menu, then choose MVC 4 Controller. However, you don't get any of the scaffolding that you do with the wizard.
  • Run devenv.exe /resetskippkgs http://msdn.microsoft.com/en-us/library/ms241276(v=vs.100).aspx
  • Disable "Visual Studio Hosting Process" in each executable project's properties (build tab, all the way at the bottom)
  • Disable Intellitrace on Debug in Tools, Options, Intellitrace 
None of the above solutions however address the actual reason that causes this problem, and no it is not a Visual Studio bug but an Operating System behavior that causes this issue. The issue is caused by the Automatic File Block feature that was adding by Microsoft in recent years. Basically, any time you download or copy files and/or folders from the web or an un-trusted source Windows automatically blocks that file or folder. If you right click on the file in question, you will see the following:


Here is a list of ways your file may get blocked:
  • Your Project is under CVS, SVN, Git or Mercurial Source Control, and you just fetched the latest version of the Project from that Source Control.
  • You downloaded the project from the WWW through a list file and extracted it, then ran the Solution File. 
  • You copied the files from a Network Location that is NOT Trusted by your PC.
All you have to do to unblock the files and folders is to click on the "Unblock" button shown in this dialog, which you can get to by right-clicking on the file and choosing "Properties" in File Explorer. Ideally you want to unblock all files and folders in your project, but the important ones are:
  • /packages/EntityFramework.5.0.0 (or whatever version you are using) recursively
  • Your Project's ".sln", ".suo", ".v11.suo", ".csproj", ".csproj.user".
  • All assemblies inside the "bin" folder.
  • The "Controllers" folder and all Controller Class files inside it. 
Make sure Visual Studio is closed and exited completely before doing this. By doing so you should be able to Add Controllers in the above manner again. 

Comments, suggestions, concerns? Leave them down below. 

Thanks,
Pete Soheil
DigiOz Multimedia, Inc.

11 comments:

Richard Evans said...

Thank you. Unblocking the downloaded zipped project also unblocked the files after extracting. Never would've thought to do that. I was chasing my tail before seeing your post.

Stephen Rajan said...

Thanks for the post, least expected it would be a file blocked that could prevent new controller being created; looks at the internal wiring up for the scaffolding I suppose.
Just to add a bit of what I did - if you have used nuget to install new libraries, it is likely that these are blocked as well and can become cumbersome to unblock, so use the powershell command -
c:> gci D:\XYZ\packages\ Unblock-File

Saves a lot of time.

DigiOz Multimedia said...

That's a great tip, thanks Stephen.

BlogBrett said...

My files have no unblock button. Still dead in the water

DigiOz Multimedia said...

Try closing the visual studio solution, delete everything in the "package" folder, then run the solution file again and let Visual Studio fetch all the required packages for you. See if that helps.

Niko Holm said...

I had the same issue and it was due to hosting the files on my NAS, and mapping all the filepaths to there (Tools > Options > Projects & Solutions > General).

The quickest solution which worked was just to keep the files on a local drive instead of the server and back them up to the nas periodically.

HTH

DigiOz Multimedia said...

Yes, the same issue happens if you use a Network Drive or a Network Attached Storage, since it is considered an external location outside your PC.

Andrew Duncan said...

@BlogBrett did you ever find a solution?

toddmo said...

Where is the packages directory normally?

DigiOz Multimedia said...

It normally is in the root of the directory you chose to save your project to.

toddmo said...

One option I see is, once you go through the turmoil of creating the first controller, when you need another one, copy and paste the first one. That does not suffer the delay.
Or just add a new class.

Can do the same with views.