Using SqlServerStorage with AddProfilerResults results in NullReferenceException

We’re taking advantage of AddProfilerResults as we use MiniProfiler on an API running on a separate service. This is great and gives us fantastic visibility on the internals of our UI and the underlying API calls.

In development locally when this is all running on one machine this is fine and dandy however once we deploy into more production-like environments the application is deployed across a cluster and is load balanced so we need to store the MiniProfiler data somewhere central. We’ve opted for SqlServerStorage as it already comes in the Nuget package but we started running into problems.

I’ve managed to track this down to the use of AddProfilerResults. If I don’t add an external profiler it runs fine. When I do add an external profiler it throws a NullReferenceException when I call MiniProfiler.Stop().

Poking around I’ve tracked it down to line 150 of SqlServerStorage.cs:

https://github.com/MiniProfiler/dotnet/blob/master/StackExchange.Profiling/Storage/SqlServerStorage.cs#L150

I’ve managed to work around the issue with a little hack but so far I end up losing the Timings of Steps within the external profiler so I still haven’t quite gotten it right. Here’s my hack currently:

bool isRoot;

try
{
    isRoot = timing.IsRoot;
}
catch (NullReferenceException)
{
    isRoot = timing.ParentTimingId == Guid.Empty;
}

And then I pass isRoot onto the Sql call.

Perhaps someone here more familiar with the internals of MiniProfiler can help point me in the right direction?