Introduction
As you may know, ASP.NET MVC 4 it's an
open source project and the code is freely available on
CodePlex.
You can download and explore it to see how they have implemented some of the classes we are interested in or just to understand a little more about the entire framework.
In my case, for example, it has been very useful to see how the
HandleError attribute is automatically serving the
Error view in case of exception.
Sometimes it could be more useful to directly enter into the source code at runtime and see which operations are performed, watch or change the values of the variables and jump from one part of the code to another.
Today I will show then how to debug a web application into the source code of ASP.NET MVC 4
without the need to download and compile the whole project from CodePlex.
In order to do this we will use the
SymbolSource server, which publicly provides the
PDB symbols required to perform
remote debugging in Visual Studio.
The PDB files are automatically created when the source code is compiled in
debug mode and they contain information used to debug the code at runtime. These files are not usually included when we download the DLL files for an external library.
To obtain the PDB files of an external library (ASP.NET MVC 4 in this case) there are two ways:
- Download and compile the source code of the library (if available)
- Use SymbolSource and remote debugging
If you want to learn more about the PDB files you can read this
this post.
Visual Studio Configuration
First you need to configure Visual Studio so that it can connect to the SymbolSource server and download the PDB files.
To do that, follow these steps:
- Go to Tools -> Options -> Debugger -> General
- Uncheck the option Enable Just My Code (Managed Only)
- Uncheck the option Enable. NET Framework source stepping (this is optional)
- Check the option Enable source server support
- Uncheck the option Require source files to exactly match the original version
The options screen should look like this:
Now you must
set the servers from which to download the PDB files:
- Go to Tools -> Options -> Debugger -> Symbols
- You should see only one item: Microsoft Symbol Servers
- Click the exlamation mark icon to the right and add the following addresses:
- http://referencesource.microsoft.com/symbols
- http://srv.symbolsource.org/pdb/Public
- http://srv.symbolsource.org/pdb/MyGet
- http://msdl.microsoft.com/download/symbols
- If needed, select a folder for the cache: in my case it is set to C:\Users\Luca\AppData\Local\Temp\SymbolCache
The screen should look like this:
Finally you need to
specify the DLL files you want to load, otherwise Visual Studio will load all the modules and debugging becomes very slow.
In this case we are only interested in ASP.NET MVC, so click on
Only specified modules, then click on
Specify modules and insert
System.Web.Mvc.dll:
Press OK and exit from the options screens.
Now we're almost done and we just need to open or create a new ASP.NET MVC 4 application, place a breakpoint and start debugging with
Debug -> Start Debugging.
If everything went well, in the Call Stack window you should see the ASP.NET MVC modules
in black:
Now when you press
Debug -> Step Into on an ASP.NET MVC statement
you will enter directly into the source code of the framework, with a lot of comments too!
Help me! It doesn't work!
If the modules in the
Call Stack window are still appearing in gray and you can't enter into the source code of the framework, you can try the following solutions:
Make sure you followed all the steps and you have checked all the required options. In particular, make sure that you have removed the check from
Require source files to exactly match the original version and entered all the SymbolSource addresses in the right order.
Try uninstalling and reinstalling the ASP.NET MVC 4
NuGet package (you don't have to reinstall the program, just the package!).
If you still cannot run the debugger, there is a final solution that worked fine for me.
It appears that in some cases the DLL files in the
GAC (Global Assembly Cache) prevent Visual Studio from loading the PDB files from the remote server.
Reinstalling ASP.NET MVC did not help in my case (I just lost a lot of time) but I solved this problem in the following way:
- Go to C:\Windows\Microsoft.NET\assembly\GAC_MSIL
- Find the folder called System.Web.Mvc
- Rename it, for example _System.Web.Mvc
In this way, the DLL files for ASP.NET MVC will not be searched in the GAC allowing you to connect to the SymbolSource server and download the PDB files.