diff --git a/DragonFruit.Common.Data/Extensions/ResponseExtensions.cs b/DragonFruit.Common.Data/Extensions/ResponseExtensions.cs index 5841be6..3d00901 100644 --- a/DragonFruit.Common.Data/Extensions/ResponseExtensions.cs +++ b/DragonFruit.Common.Data/Extensions/ResponseExtensions.cs @@ -8,20 +8,30 @@ namespace DragonFruit.Common.Data.Extensions { public static class ResponseExtensions { - public static T To(this HttpResponseMessage response) + public static T To(this HttpResponseMessage response, bool disposeResponse = true) { - response.EnsureSuccessStatusCode(); + try + { + response.EnsureSuccessStatusCode(); - var targetType = typeof(T); + var targetType = typeof(T); - switch (targetType.IsPrimitive) - { - case true: - case false when targetType == typeof(string): - return (T)Convert.ChangeType(response.Content.ReadAsStringAsync().Result, targetType); + switch (targetType.IsPrimitive) + { + case true: + case false when targetType == typeof(string): + return (T)Convert.ChangeType(response.Content.ReadAsStringAsync().Result, targetType); - default: - throw new ArgumentException($"Cannot convert HTTP response to {targetType}. It must be a primitive type or a string", nameof(T)); + default: + throw new ArgumentException($"Cannot convert HTTP response to {targetType}. It must be a primitive type or a string", nameof(T)); + } + } + finally + { + if (disposeResponse) + { + response.Dispose(); + } } } }