Debugging Deeper through Reference Source

Debugging Deepr

A little over a month ago, the ASP.NET team announced several changes and updates to the .NET Reference Source and in this post we are going to discuss how to actually integrate it into Visual Studio so that you can step into the actual .NET Framework source when debugging your applications.

What is Reference Source again?

Reference Source, if you are unfamiliar with it, was a project that Scott Guthrie and his team started back in 2007 in hopes of releasing the .NET Framework source to allow developers to easily reference it (without all kinds of decompilation and mischief) to see what was really going on under the hood. Shortly after that release, the team made a few changes that would allow developers to actually step through the source code which was a major step in a very cool direction.

One of the major difficulties with managing something that is constantly evolving like the .NET Framework is purely the fact that it is “constantly evolving”. Updating documentation usually takes time and with a project of this magnitude, it could simply be something that is “left over until the end” or simply doesn’t get done :

Reference Source circa 2007.

Reference Source circa 2007 via Scott Guthrie

Reference Source Meets Roslyn.

As part of this year’s announcement regarding Reference Source, it was mentioned that it would be joining forces with Microsoft’s latest development wonder, Roslyn.

Roslyn is a managed compiler-as-a-service that has really flipped the script in the .NET world recently. It provides all kinds of wonderful features that were simply not possible in previous years and has already been used not only by Microsoft but in many other arenas such as .NETFiddle, Bing Code Search, Semantic Merge and more. Roslyn was used to help generate a semantic index of the entire .NET Framework source to allow it to be searched through with the greatest of ease.

Currently, .NET 4.5.1 is indexed and readily available on the Reference Source site and the ASP.NET team announced a commitment to keep things updated with each upcoming release as they occur to prevent any stagnation that may have plagued the previous versions of the tool. So you can be relatively sure that whenever you are accessing the latest version of the Reference Source that it should be the latest and greatest. The improvements were not limited to just performance either.

The UI received a very stylish overhaul as well and yield some of the nicest looking documentation that you’ll come across :

The New and Improved Reference Source

The New and Improved Reference Source

Another undocumented feature is the tiny snippets of entertaining comments that you can find scattered throughout the source as well :

An example of one of the many entertaining comments throughout the source.

An example of one of the many entertaining comments throughout the source.

Putting it to Good Use.

Let’s actually put the Reference Source to use along with Visual Studio 2013 and use it to debug an application by not only stepping through our own code, but into the source of the .NET Framework as well.

To get started, there are a few changes we need to make within Visual Studio that will allow us to target the Reference Source (which will primarily consist of enabling and disabling a bunch of properties in the Options menu).

Open up Visual Studio and navigate on over to the options menu (Tools > Options > Debugging > General) as seen below :

You'll need to enable and disable the following options within the Debugging Options in Visual Studio

You’ll need to enable and disable the following options within the Debugging Options in Visual Studio

and then you are going to disable the following options :

  • Just My Code
  • Step over Properties and Operators (Managed Only)
  • Require Source Files to Exactly Match the Original Version

and enable these ones :

  • Enable .NET Framework Source Stepping
  • Enable Source Server Source

After making the changes, your options menu should look like this :

This is what your Debugging Options should look like prior to stepping through Reference Source

This is what your Debugging Options should look like prior to stepping through Reference Source

Then, you’ll need to make sure that when debugging that you are targeting the actual Visual Studio Reference Source. You can do this by visiting the Symbols area under options (Tools > Options > Debugging > Symbols) :

You'll need to access the Symbols area in order to use Reference Source when debugging

You’ll need to access the Symbols area in order to use Reference Source when debugging

From here, you’ll want to target the Reference Source symbols available at http://referencesource.microsoft.com/symbols. You’ll need to click the Add Symbols option within the Symbols area and add the previously mentioned URL :

Click the Folder icon and add the appropriate symbols reference for Reference Source

Click the Folder icon and add the appropriate symbols reference for Reference Source

What I have found to be a safer and more reliable approach however, is to simply download the source and reference it locally from the following location :

http://referencesource.microsoft.com/DotNetReferenceSource.zip

After making those changes, you’ll need to ensure that the project that you are going to be debugging is targeting .NET 4.5.1. Debugging through Reference Source is going to currently be limited to 4.5.1 and above since those are the only actual versions of the .NET source that have been indexed so far.

Looking Under the Hood.

Now that we have configured everything, let’s make a really simple program to demonstrate traveling through the source.

// Generate a collection of values (1-100)
var numbers = Enumerable.Range(1, 100);
// Order them randomly
numbers = numbers.OrderBy(n => Guid.NewGuid());
// Store these values in an array
var numberArray = numbers.ToArray();
// Sort the array
Array.Sort(numberArray);

Using the simple program above, we will create a collection of numbers, randomly order them, store them in an array and then finally sort them using a variety of methods.

Let’s place a breakpoint on the first line and run the program.

When you hit your first breakpoint, right-click on the method (in this case System.Linq.Enumerable.Range) and you should see an option within the context menu called “Step Into Specific” which will allow you to send the debugger into the .NET source for that particular method as seen below :

You can use the "Step into Specific > (Your Method)" option to debug through the .NET source.

You can use the “Step into Specific > (Your Method)” option to debug through the .NET source.

After selecting the method to step into through “Step into Specific”, you’ll see that the debugger jumps into the related .NET source and you can step through the method as you would expect within any other .NET application being debugged :

After using the "Step into Specific" option, you'll be presented with the source for your specific function, which you can debug as expected.

After using the “Step into Specific” option, you’ll be presented with the source for your specific function, which you can debug as expected.

And that’s basically all you need to know about using Reference Source within Visual Studio to debug your applications. You should be able to jump into any of the assemblies that are currently supported within Reference Source without any issue.

Considerations

A few other considerations if you are having trouble :

  • Debugging through Reference Source currently ONLY works for full versions of Visual Studio 2013 (sorry no Express versions). I’ve spoken with several members of the Visual Studio team and they are looking into possibly removing this restriction in the future.
  • Ensure that the assembly that you are attempting to step into is one of the available assemblies for debugging mentioned here.

If you still continue to encounter any errors or something isn’t working that you believe should – contact the Reference Source Feedback team via the “Feedback” link on the Reference Source page.

But How Do I Learn ASP.NET?

“How do I learn ASP.NET?” or “What is the best way to learn ASP.NET?” are two questions that I am frequently asked on nearly daily basis. These questions are fairly subjective and as everyone should know, there is never any “best” way to learn, but I thought I would share some of my thoughts on the topic and provide some resources for those looking to delve into the .NET world.

how-do-i-learn-dotnet

Languages

Since you need to actually know a language (at least one for humans and one for computers) before you begin your journey, it’s probably important to have an idea of what your choices are. There are primarily three main languages that you’ll encounter most often in the .NET world, so I’ll provide a few resources for each of the major ones to help you on your way :

C#

A modern, object-oriented, general purpose programming language with strong similarities to Java (if you are familiar with Java) and likely the most common language that you will see when working in the .NET world. C# is incredibly versatile and easy-to-learn and is widely supported. You should have no trouble finding examples for nearly any scenario that you would need in C#.

Visual Basic

Another object-oriented language that may be quite a bit more elusive than C# with regards to being seen or used in many modern projects. Visual Basic has a syntax that you could argue is “all its own” and it may be a bit foreign to those coming from other languages like C, C++ or Java. Nevertheless, it is still a popular language and it is still being widely used today and while it wouldn’t be my first recommendation for a .NET language to pursue, it is an option.

F#

One of the most elusive of the major .NET languages is F# and this is probably due to the fact that it is a functional language (although it can encompass object-oriented and imperative concepts as well) and it the youngest of the .NET languages. It has began to gain more traction in recent years however it would receive a bronze medal if a popularity contest was held between the three languages listed here. But if you are feeling adventurous or come from a functional programming background, it is worth checking out.

You’ll also need an actual development environment to run these in. Thankfully, Microsoft offers a free “Express” version of Visual Studio (its flagship IDE) completely free for personal and commercial usage, which you can download from the link provided below :

Books

Books typically aren’t the first thing to come to mind when searching for resources on learning a technology, but people wouldn’t write them if they didn’t work. For you page-turners out there that learn best by being away from a screen and keyboard, I’ll provide a few highly recommended options that should give you a jump start into the .NET world :

Beginning ASP.NET 4.5: in C# and Visual Basic (Beginner Friendly)

This tutorial-centric book provides a great introduction to ASP.NET and is a perfect book for both developers that are new to .NET or those that may have worked with it previously as a refresher. The author does an excellent job of not only providing code to the reader, but thoroughly explaining why the code is there and how it works. (This is also a great choice as it provides both routes in C# and Visual Basic for those that are familiar with either of the languages)

Professional ASP.NET 4.5 in C# and VB (Beginner Friendly)

Don’t let the “Professional” in the title scare you away if you are just learning ASP.NET, as this title is a great choice for an introductory book to .NET or for those that have some experience under their belts. The superstar team of authors focuses on many of the foundational concepts throughout ASP.NET and covers all of the major features available within the technology so that the reader will feel confident tackling applications both big and small.

Beginning ASP.NET Web Pages with WebMatrix (Beginner Friendly)

This book is undoubtedly the best one that I have come across on ASP.NET’s Web Pages technology that uses the WebMatrix platform, which is excellent for those just getting started in .NET. This book is extremely comprehensive as well and covers not only ASP.NET specifics, but many of the tangent web development technologies like HTML, CSS, Javascript, AJAX and basically everything you would need to develop complete and dynamic web sites.

Pro ASP.NET MVC 4 (Some ASP.NET knowledge recommended)

Adam Freeman (and Steven Sanderson) provide an excellent introduction to ASP.NET MVC and hold your hand as your create your first MVC application and then things begin to rev up a bit in a title that covers MVC in a very comprehensive way. The book not only does a superb job of explaining how MVC applications operate and how to handle just about every situation that might arise when developing an MVC app.

Professional ASP.NET MVC 4(Some ASP.NET knowledge recommended)

Written by a team of rock-stars in the .NET world, Professional ASP.NET MVC 4 is a great primer to get started with ASP.NET MVC after you have a bit of foundational work under your belt. Much like the previous title, this work covers MVC in its entirety and goes into a bit greater depth about topics that are close tangents to MVC such as jQuery, AJAX calls, dependency injection and more.

As you can see, the MVC-specific books by nature are going to have a slightly larger learning curve than a traditional “Web Forms” approach. If you are a quick learner and already have some web development experience, then you should feel right at home jumping into them, but if you are looking for titles to hold your hand and explain ASP.NET in its entirety, then you may be better off with one of the “Beginner Friendly” books above.

Resources and Tutorials

For some reading can be one of the best ways to absorb the concepts when learning a new technology, however I have found that developers tend to learn best when they actually can sit in front of a keyboard and “develop”. I’ve always thought that the best way to learn to become better at something is to do that very thing and software development is not exempt from this rule.

I’ve compiled a list of different resources and tutorial series that I would recommend anyone that is looking to learn or become more proficient in ASP.NET to consider below :

Getting Started with ASP.NET

If I only had one resource to name on this entire post, this would be it.

Coincidentally, the first resource on the list just so happens to be one that shares the name with the technology itself (asp.net) so you can probably bet that it is going to be a pretty reliable place to visit. The Getting Started area of the ASP.NET site has everything a developer of any level would need to further their knowledge: tutorials, video series, walk-throughs, examples, discussions and more. All of the content was written and developed by leading members of the .NET community or actual members of the .NET development team and the quality shows.

Microsoft Events and Hands-On Labs

An often overlooked resources for those looking to get some “hands-on” experience (even though they are through videos) is Microsoft’s Events site. It features full-length lab sessions and webcasts that cover a variety of topics for developers of all experience levels, which is great for those that prefer a more structured, instructor-led learning environment. There are 500+ different events listed under ASP.NET and the events can be accessed either on-demand or you can even participate in a live lab in progress (assuming you schedule to attend it) via web cast.

Channel 9

Channel 9 is Microsoft’s video archive of all things development and it contains thousands of videos that cover just about every aspect of Microsoft development and technology. You can find video tutorials, explanations and overviews on ASP.NET and any of its underlying sub-categories (Web Forms, MVC, WebMatrix, Web API, SignalR, SPA and more) all in an extremely easy to use and follow format that allows the viewer to easily “skip to the good parts”.

Microsoft’s Beginners Development Center

Microsoft’s Beginners Development Center is another excellent resource that not only provides a series of tutorials for ASP.NET, but it also features a crash-course on Web Development in general including an introduction to HTML, Javascript, CSS and more. If you continue to explore around the site, you’ll see that it isn’t restricted to any particular area of development and has content for web applications, desktop applications, web services and more.

Microsoft Virtual Academy

Another learning resources available from the folks at Redmond is the Microsoft Virtual Academy. The academy is a web-based, course-driven learning environment that has full courses that cover just about every major topic and technology covered by Microsoft. If you are looking to learn more about .NET or any Microsoft technology and you learn best using structured courses, then this is certainly worth checking out.

Any of the above resources would be excellent areas to focus your learning and I would recommend scouring through each of them, as you would be surprised how the teaching styles of different instructors might help you learn or understand something more effectively. I will mention it again however – if you want the most comprehensive place to look for learning ASP.NET, then visit ASP.NET 🙂

Supplementary Learning

While tutorials and books are excellent resources, there are a few other resources that I would recommend (if you are interested) that cover not only ASP.NET but development in general and would likely be worth checking out :

PluralSight

If you are a developer and you like instructor-led training through well-designed courses, taught by people that know what they are talking about, then you need to seriously consider investing in a PluralSight subscription. PluralSight is the premier developer training site not only for learning about .NET or Microsoft-related technologies in general, but ANYTHING development related. The courses cover a wide-range of categories and range from “FizzBuzz” and “Hello World” level to designing scalable enterprise-level system architecture.

Code Academy

Code Academy is a great resource to learn many of the the supplementary skills and technologies that are associated with ASP.NET such as Javascript, HTML, CSS, jQuery and more. These may play a bit less of a factor when you are first getting started (especially if you are working with Web Forms) but as you migrate to more advanced applications and want a bit more control over how things look, these are valuable skills to pick up.

DotNetFiddle

DotNetFiddle is a great tool to tinker with basic .NET Applications from the comfort of your browser. It supports all of the languages listed above (C#, F# and Visual Basic) and has support for easily integrating NuGet packages into your applications as well.