Tuesday, April 2, 2013

Debugging into ASP.NET MVC 4 source code

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.


14 comments:

  1. Thanks Luca. I have been wasting a lot of time trying to make this work and I wish the SymbolSource guys could have mentioned the GAC issue on their web site and saved a lot of people a lot of time,

    Do you know if there is any way to download all the Microsoft symbols in one big download? I was trying to step into WebApi yesterday and although most of the symbols downloaded I got stuck at System.Net.Http.

    I noticed that there are multiple versions of it, one in the GAC and one in the 4.5 framework folder. If I renamed the GAC version as per your post I couldn't even start the site. I get an exception that the assembly could not be found.

    Thanks again for your post. I helped and saved me a lot of time.

    ReplyDelete
    Replies
    1. Hi Thomas, and thanks for your comment!

      I was able to step into the WebApi source code by clicking on "Only specified modules" in the Symbols option screen and inserting System.Web.Http.dll. On the Call Stack windows the DLL was still gray but I right-clicked on it and selected "Load Symbols From -> Microsoft Symbol Servers".

      Delete
  2. Yes, I was too but once I reach code that references System.Net.Http it's game over. If you put a break point in app start and start stepping into GlobalConfiguration you will reach code that requires System.Net.Http pretty quickly where it just shows you the Source Unavailable screen which is terribly annoying. If you ever manage to solve this issue or any similar issue, please let me know. Thanks again.

    ReplyDelete
    Replies
    1. Hi Thomas, you could try to check the "Enable .NET Framework source stepping" option. Where exactly is the line where you are stuck?

      Delete
  3. You can put a break point anywhere in your controller, then you will see that any calls to System.Net.Http in the call stack are grayed out (in my case they are as I am only loading from symbolsource servers as I was testing this). You can double click in the call stack on any System.Net.Http call and select load from Microsoft Servers. Once loaded you can try to double click to actually get to the source code which in my case will not work as I get the Source Not Available message.

    ReplyDelete
  4. I got asp.net mvc 4 source code from Codeplex site and setup debugging on my system. Here is the step by step instructions on how to do it. (url: http://vijayt.com/Post/Setting-up-aspnet-mvc-for-debugging-in-your-system) Hope it is useful.

    ReplyDelete
  5. Add following symbol path for MVC :

    http://srv.symbolsource.org/pdb/Public

    ReplyDelete
  6. Oh joy, I get to type my entire comment again from scratch since I wasn't already logged into Google+ it seems. *sigh*

    I was just curious why it was necessary to add the 4 additional symbol servers that you suggest in your blog post. I already had checked "Microsoft Symbol Servers" but VS was not finding the System.Web.Mvc.pdb that it needed to allow me to step into the appropriate .cs source code. After I stumbled across your blog and added the 4 additional public symbol servers it worked. It just seems silly that Microsoft's own public symbol servers fail at providing something that should be available for download.

    ReplyDelete
  7. simply superb,mind blowing, i will share your blog to my friends also
    .NET Online Training Hyderabad

    ReplyDelete
  8. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete