How to Debug and Step Into Service Fabric SDK Source Code
In this blog, Premier Consultant Kurt Schenk shows how to debug into the source code of the Service Fabric SDK which is used to develop Reliable Services and Reliable Actors. Service Fabric is open source, including the SDK which is on GitHub here.
#1. Determine what commit to check out of GitHub
You need to match the source code that you check out with the NuGet package that you are referencing. For example, using Visual Studio we can see that my service is referencing these Service Fabric NuGet packages.
This is 6.4 CU6 release of Service Fabric, on May 2nd, 2019, which you can verify by going to https://docs.microsoft.com/en-us/azure/service-fabric/release-notes and clicking on “release notes” in section “Service Fabric 6.4 releases” for May 2, 2019. You will see the following table showing that this release is compatible with Service Fabric .NET SDK (3.3.658) / Microsoft.ServiceFabric (6.4.658)
If you are referencing the latest version (Major.Minor) of a Service Fabric release (6.4, 6.5, 7.0, 7.1, https://docs.microsoft.com/en-us/azure/service-fabric/release-notes ) then you can check out the release branch as shown below. So branch release_3.3 is 6.4 CU7 released May 28, 2019 which is .NET SDK (3.3.664) / Microsoft.ServiceFabric (6.4.664).
If you navigate to https://github.com/microsoft/service-fabric-services-and-actors-dotnet/blob/release_3.3/properties/service_fabric_common.props you will see the same confirmed there.
<NugetPkg_Version_Microsoft_ServiceFabric>6.4.644</NugetPkg_Version_Microsoft_ServiceFabric> <NugetPkg_Version_Microsoft_ServiceFabric_Diagnostics_Internal>3.3.644</NugetPkg_Version_Microsoft_ServiceFabric_Diagnostics_Internal> <NugetPkg_Version_Microsoft_ServiceFabric_Data>3.3.644</NugetPkg_Version_Microsoft_ServiceFabric_Data> <NugetPkg_Version_Microsoft_ServiceFabric_FabricTransport_Internal>3.3.644</NugetPkg_Version_Microsoft_ServiceFabric_FabricTransport_Internal> <MajorVersion>3</MajorVersion> <MinorVersion>3</MinorVersion> <BuildVersion>17</BuildVersion> <Revision>0</Revision
However, in my sample project I am using a slighter earlier version of the Service Fabric SDK and therefore have to go back a few commits, from 6.4.664 back 6.4.658. To find an exact match, look at AssemblineInformationVersion for one of the Service Fabric SDK assemblies referenced in the service, and match to MajorVersion.MinorVersion.BuildVersion.Revision in service_fabric.common.props. For example, looking at bin\x64\Release\Microsoft.ServiceFabric.Actors.dll with ILSpy, I see AssemblineInformationVersion 126.96.36.199.
[assembly: TargetFramework(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")] [assembly: AssemblyCompany("Microsoft")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright (c) Microsoft Corporation. All rights reserved.")] [assembly: AssemblyFileVersion("188.8.131.52")] [assembly: AssemblyInformationalVersion("184.108.40.206")] [assembly: AssemblyProduct("Microsoft Azure Service Fabric")] [assembly: AssemblyTitle("Microsoft.ServiceFabric.Actors")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("220.127.116.11")]
Going back a bit in commit history there is a match here: https://github.com/microsoft/service-fabric-services-and-actors-dotnet/blob/fe334251815b33aa3d29f243d0c9a2ae5ea8d8d6/properties/service_fabric_common.props.
Now we can check out commit fe334251815b33aa3d29f243d0c9a2ae5ea8d8d6 and this will be an exact match for the Service Fabric SDK source code.
- Download and install GitHub Desktop (https://desktop.github.com/)
- Go to the repo at https://github.com/microsoft/service-fabric-services-and-actors-dotnet.
- Clone repository, Open in Desktop
- Set the local path for the repo
- The repo has the latest commit from June 1st, 2020
- Open command prompt in Git Desktop
- Checkout commit above
- We can see we have the version we were looking for in service-fabric-services-and-actors-dotnet\properties\service_fabric_common.props
- To go go back to the most recent local commit type git checkout develop
Git for Windows (https://gitforwindows.org/) is a great tool that you could use for this as well.
#2. Run code that will execute the code path that you want to test
This will ensure that the that the relevant assemblies have loaded into the process. In my case, I go to http://localhost:8181/api/Actors/1 in my browser, and the ASP.NET Core controller then calls a Service Fabric Actor service using an ActorProxy andService Fabric Remoting. The Actor method throws a custom FabricNotReadableException, MyFabricTransientException(“My custom FabricTransientException”), and after some retries the ActorProxy fails and throws an exception.
#3 Attach to the service you want to debug
Now attach to the service, which in my case is a Stateless Service called StatlessWebApp2 that has the ASP.NET Core code I want to debug. I am using Visual Studio 2019, with a local development cluster.
#4. Load Service Fabric SDK assemblies
You may need to set the symbol path in Visual Studio. To do that go File Menu, to Debug\Options\, then to Debugging\Symbols. And make sure that Microsoft Symbol Servers checkbox is selected. You could add a reference to symbols for your code as well here. If you have set _NT_SYMBOL_PATH environment variable, which I hav, it will show up in the UI here as well.
In order for your breakpoint to be hit, you have to be sure that the Service Fabric SDK symbols are loaded. Since you executed the code path you are interested in (#2), now you can go to Debug/Windows/Modules and load the symbols needed. You will see Symbol Status: Symbols loaded if this successful. Symbols may not be found for some assemblies, like Microsoft.ServiceFabric.Internal.dll, which does not have public symbols.
#5. Set a breakpoint in the Service Fabric SDK
Now you are ready to find the source file you are interested in and set a breakpoint. For example in \src\Microsoft.ServiceFabric.Services.Remoting\Client\ServiceRemotingExceptionHandler.cs in order to see how certain exceptions are handled on the client side.
#6. Execute Code that will hit the breakpoint set in the Service Fabric SDK
When I navigate to http://localhost:8181/api/Actors/1 again, this code on the client side is hit in the Service Fabric SDK source code. Now I can inspect variables and step through the Service Fabric SDK source code.