Looking at .NET Source code from Visual Studio

Its been a number of days when I last posted in DailyDotnetTips, but I always find fun in posting something here. Today I will talk about how you can get integration of Source code server with your .NET programs so that you can easily see the Source code of the .NET library while you debug. By source code I mean the actual Source code which is written by Microsoft people not something that we disassemble with Reflector or some other tool. The source code will have all the comments available, the debug symbols, the program db and everything. I am talking in steps how to configure your Visual Studio to get Source from Microsoft servers.

Microsoft Officially released the source code of .NET framework for you which you can either download or get while  you debug your application.

Steps to install

1. Open Visual Studio and move to Tools -> Options -> Debugging as shown in the figure below :

We need to check the Enable .NET Framework source stepping.  It is evident from the messagebox that pops up is actually disabling Just My Code for your visual studio.

2. Now move to Debugging -> Symbols and select Microsoft Symbol Server.

Here we choose Microsoft Symbol servers to load the Source code files to your visual studio. Remember if you do this, there will be slight latency when you step into any .NET library. This latency is due to the fact that Visual studio downloads the file from the Microsoft Source code server. You can also specify the modules for which you dont want to include symbols (note : if you do not load symbols of a particular dll, you cannot download source file for that particular dll)

3.  Click OK, as your setup is now complete.

Remember it will take some time, as it is going to cache the Symbols for the first time so that when you request for the source code while you debug your application it can get it perfectly. Visual studio can easily identify the source code file name, the line number for which the source code is requested etc to give you really great experience with Visual Studio. Its  awesome. You will see few things will go on to your visual studio during these phaze.

In the image above the Visual studio shows that it loads public symbols for System.Deployment.dll.

Now moving back to our source code lets write a simple code and show you how to step through your code.

class Program
static void Main(string[] args)
Console.WriteLine("Hello Source code ");

Now lets run this code and put a breakpoint on Console.WriteLine.  Now when I am in Debug mode select call Stack and right click on the dll and choose Load Symbols for the project.

It will show up a dialog to accept the licence agreement for the Source code as below :

Once you accept the agreement, you will get the source code and step down the source code for Microsoft.

You can see the Console.WriteLine moves to the static WriteLine method at line number 888 which actually the Console.WriteLine code written by Microsoft in mscorlib.dll.

If you see something like the image below, I think the file is not available for that particular dll.

Microsoft actually updates all the source code to the Reference server with the new release of .NET. If you are running very new (CTP) products, it is possible that the source files are not available to you and you see this dialog.

If you are running Visual Studio 2008, Microsoft has also released a QFE (Quick Fix Engineering) which you can download and install to your Visual studio to get the same flavor. You can read Shawn Burke’s Post here to do it for Visual Studio 2008.

I hope you would love this post.

Stay tune for more updates.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434