Last week I read this tweets from Maxime Rouiller (@MaximRouiller):

Then I found out that SourceLink is a language-and source-control agnostic system for providing first-class source debugging experiences for binaries."

What that means is that, once you enable SourceLink, the users are able to step into your code without effort! from both Visual Studio and Visual Studio Code which are ready to let you enjoy the experience.

This findings triggered my interest and started wondering on how could I add such a nice feature to the netDumbster project.

Here is what I did:

Added the following properties to the project’s csproj

1<PublishRepositoryUrl>true</PublishRepositoryUrl>
2<EmbedUntrackedSources>true</EmbedUntrackedSources>
3<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

Added the following ItemGroup to the project’s csproj

1<ItemGroup>
2  <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="All"/>
3</ItemGroup>

Note: this Package Reference depends on the source control system.

Finally built the nuget package as usual

1dotnet pack -c release

Believe me that was all!!! So then I tested the new package and features with a simple console application and Visual Studio Code:

Created a new console project

1mkdir SourceLinkTest
2cd SourceLinkTest
3dotnet new console
4dotnet add package netDumbster -v 2.0.0.3
5dotnet restore

Replaced the contents of Program.cs with the following code

 1using System;
 2using netDumbster.smtp;
 3using netDumbster.smtp.Logging;
 4
 5namespace a
 6{
 7    class Program
 8    {
 9        static void Main(string[] args)
10        {
11            LogManager.GetLogger = type => new ConsoleLogger(type);
12            SimpleSmtpServer.Start();
13        }
14    }
15}

Generated Assets for Build and Debug for .NET

Inside Visual Studio Code and from the Command Palette invoke the .NET: Generate Assets for Build and Debug command so it creates the launch.json and tasks.json files inside the .vscode folder.

Replace the contents of launch.json with the following configuration

 1{
 2   // Use IntelliSense to find out which attributes exist for C# debugging
 3   // Use hover for the description of the existing attributes
 4   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
 5   "version": "0.2.0",
 6   "configurations": [
 7        {
 8            "name": ".NET Core Launch (console)",
 9            "type": "coreclr",
10            "request": "launch",
11            "preLaunchTask": "build",
12            // If you have changed target frameworks, make sure to update the program path.
13            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/SourceLinkTest.dll",
14            "args": [],
15            "cwd": "${workspaceFolder}",
16            // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17            "console": "internalConsole",
18            "stopAtEntry": false,
19            "internalConsoleOptions": "openOnSessionStart",
20            "justMyCode": false,
21            "suppressJITOptimizations": true
22        },
23        {
24            "name": ".NET Core Attach",
25            "type": "coreclr",
26            "request": "attach",
27            "processId": "${command:pickProcess}"
28        }
29    ,]
30}

Note: the configuration disables justMyCode and enables suppressJITOptimizations.

I added a breakpoint in line 12 of Program.cs and started debugging. Once the debugger hit the breakpoint I pressed F11 to “Step Into” the netDumbster code. And surprise! I was in!!!

image shows the debugger inside netDumbster’s code

Now let me ask you something: What are you waiting to get out there and add SourceLink to your existing library?

Hope you enjoyed the ride!