From c136595e678d65673e9dc1834ae063fef0f2a8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0vihl=C3=ADk?= Date: Fri, 5 Feb 2021 15:42:24 +0100 Subject: [PATCH] fixes #264 --- .../Extensions/HttpRequestHeadersExtensions.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Kentico.Kontent.Delivery/Extensions/HttpRequestHeadersExtensions.cs b/Kentico.Kontent.Delivery/Extensions/HttpRequestHeadersExtensions.cs index 6cc17bec..65850a9e 100644 --- a/Kentico.Kontent.Delivery/Extensions/HttpRequestHeadersExtensions.cs +++ b/Kentico.Kontent.Delivery/Extensions/HttpRequestHeadersExtensions.cs @@ -92,11 +92,13 @@ internal static string GetSdk() internal static string GetSource() { Assembly originatingAssembly = GetOriginatingAssembly(); - - var attribute = originatingAssembly.GetCustomAttributes().FirstOrDefault(); - if (attribute != null) + if (originatingAssembly != null) { - return GenerateSourceTrackingHeaderValue(originatingAssembly, attribute); + var attribute = originatingAssembly.GetCustomAttributes().FirstOrDefault(); + if (attribute != null) + { + return GenerateSourceTrackingHeaderValue(originatingAssembly, attribute); + } } return null; } @@ -127,10 +129,11 @@ internal static string GenerateSourceTrackingHeaderValue(Assembly originatingAss internal static Assembly GetOriginatingAssembly() { var executingAssembly = Assembly.GetExecutingAssembly(); + // Get the whole stack trace, get involved assemblies, and determine which one references this SDK var callerAssemblies = new StackTrace().GetFrames() .Select(x => x.GetMethod().ReflectedType?.Assembly).Distinct().OfType() .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName == executingAssembly.FullName)); - var originatingAssembly = callerAssemblies.Last(); + var originatingAssembly = callerAssemblies.Any() ? callerAssemblies.Last() : null; return originatingAssembly; } }