Skip to content

Commit 77046fb

Browse files
authored
fix: fix null reference exception when it's not possible to get runtime version from the assembly (#851)
1 parent 054d3e1 commit 77046fb

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Box.V2/Managers/BoxResourceManager.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using System.Reflection;
1717
using System.Text;
18+
using System.Text.RegularExpressions;
1819
using System.Threading.Tasks;
1920

2021
namespace Box.V2.Managers
@@ -379,12 +380,21 @@ private string CheckFor45PlusVersion(int releaseKey)
379380
private string GetNetCoreVersion()
380381
{
381382
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
382-
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
383-
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
384-
return netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2 ?
385-
assemblyPath[netCoreAppIndex + 1] :
386-
null;
387-
}
383+
if (assembly?.CodeBase != null)
384+
{
385+
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
386+
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
387+
return netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2 ?
388+
assemblyPath[netCoreAppIndex + 1] :
389+
null;
390+
}
388391

392+
#if NETSTANDARD2_0
393+
var frameworkVersion = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
394+
return Regex.Match(frameworkVersion, @"\d+(\.\d+)+").Value;
395+
#else
396+
return null;
397+
#endif
398+
}
389399
}
390400
}

Box.V2/Request/HttpRequestHandler.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ private class ClientFactory
269269

270270
private static HttpClient CreateClient(bool followRedirect, IWebProxy webProxy)
271271
{
272-
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip };
272+
var handler = new HttpClientHandler();
273+
if (handler.SupportsAutomaticDecompression)
274+
{
275+
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
276+
}
273277
handler.AllowAutoRedirect = followRedirect;
274278

275279
if (webProxy != null)

0 commit comments

Comments
 (0)