From 0e74d65daf08c6ca5d9b2147581bf4ae3312f17c Mon Sep 17 00:00:00 2001 From: James Skimming Date: Fri, 28 Aug 2015 14:16:18 +0100 Subject: [PATCH] Correctly dispose objects to release connections During live usage I discovered the maximum HTTP connections (defaults to 2) were excused, as a result the application hung on the third request. The fix is to correctly dispose of the HTTP request/response objects, something I thought unnecessary for HttpClient. --- .../HttpClientDnsRecordExtensions.cs | 18 +-------- src/CloudFlare.NET/HttpClientExtensions.cs | 39 ++++++++++++++++--- .../HttpClientZoneExtensions.cs | 37 ++---------------- 3 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs b/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs index 35fffe0..2390f32 100644 --- a/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs +++ b/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs @@ -17,31 +17,17 @@ public static class HttpClientDnsRecordExtensions /// Gets the zones for the account specified by the details. /// /// - public static async Task> GetDnsRecordsAsync( + public static Task> GetDnsRecordsAsync( this HttpClient client, IdentifierTag zoneId, CancellationToken cancellationToken, CloudFlareAuth auth) { - if (client == null) - throw new ArgumentNullException(nameof(client)); if (zoneId == null) throw new ArgumentNullException(nameof(zoneId)); - if (auth == null) - throw new ArgumentNullException(nameof(auth)); Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records"); - var request = new HttpRequestMessage(HttpMethod.Get, uri); - request.AddAuth(auth); - - HttpResponseMessage response = - await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) - .ConfigureAwait(false); - - return (await response - .GetResultAsync>(cancellationToken) - .ConfigureAwait(false)) - .Result; + return client.GetAsync>(uri, auth, cancellationToken); } } } diff --git a/src/CloudFlare.NET/HttpClientExtensions.cs b/src/CloudFlare.NET/HttpClientExtensions.cs index 0fcaebd..e41c4cc 100644 --- a/src/CloudFlare.NET/HttpClientExtensions.cs +++ b/src/CloudFlare.NET/HttpClientExtensions.cs @@ -13,11 +13,6 @@ /// public static class HttpClientExtensions { - /// - /// Gets the base address of the CloudFlare API. - /// - public static Uri ZonesUri { get; } = new Uri(CloudFlareConstants.BaseUri, "zones"); - /// /// Gets the of a CloudFlare API . /// @@ -60,5 +55,39 @@ public static void AddAuth(this HttpRequestMessage request, CloudFlareAuth auth) request.Headers.Add("X-Auth-Key", auth.Key); request.Headers.Add("X-Auth-Email", auth.Email); } + + /// + /// Executes a request returning the type specified by . + /// + /// The type of the . + public static async Task GetAsync( + this HttpClient client, + Uri uri, + CloudFlareAuth auth, + CancellationToken cancellationToken) + where T : class + { + if (client == null) + throw new ArgumentNullException(nameof(client)); + if (uri == null) + throw new ArgumentNullException(nameof(uri)); + if (auth == null) + throw new ArgumentNullException(nameof(auth)); + + using (var request = new HttpRequestMessage(HttpMethod.Get, uri)) + { + request.AddAuth(auth); + + HttpResponseMessage response = + await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false); + using (response) + { + CloudFlareResponse cloudFlareResponse = + await response.GetResultAsync(cancellationToken).ConfigureAwait(false); + return cloudFlareResponse.Result; + } + } + } } } diff --git a/src/CloudFlare.NET/HttpClientZoneExtensions.cs b/src/CloudFlare.NET/HttpClientZoneExtensions.cs index f001d29..023b2d7 100644 --- a/src/CloudFlare.NET/HttpClientZoneExtensions.cs +++ b/src/CloudFlare.NET/HttpClientZoneExtensions.cs @@ -17,65 +17,36 @@ public static class HttpClientZoneExtensions /// Gets the zones for the account specified by the details. /// /// - public static async Task> GetZonesAsync( + public static Task> GetZonesAsync( this HttpClient client, CancellationToken cancellationToken, CloudFlareAuth auth, PagedZoneParameters parameters = null) { - if (client == null) - throw new ArgumentNullException(nameof(client)); - if (auth == null) - throw new ArgumentNullException(nameof(auth)); - Uri uri = new Uri(CloudFlareConstants.BaseUri, "zones"); if (parameters != null) { uri = new UriBuilder(uri) { Query = parameters.ToQuery() }.Uri; } - var request = new HttpRequestMessage(HttpMethod.Get, uri); - request.AddAuth(auth); - - HttpResponseMessage response = - await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) - .ConfigureAwait(false); - - return (await response - .GetResultAsync>(cancellationToken) - .ConfigureAwait(false)) - .Result; + return client.GetAsync>(uri, auth, cancellationToken); } /// /// Gets the zones for the account specified by the details. /// /// - public static async Task GetZoneAsync( + public static Task GetZoneAsync( this HttpClient client, IdentifierTag zoneId, CancellationToken cancellationToken, CloudFlareAuth auth) { - if (client == null) - throw new ArgumentNullException(nameof(client)); if (zoneId == null) throw new ArgumentNullException(nameof(zoneId)); - if (auth == null) - throw new ArgumentNullException(nameof(auth)); Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}"); - var request = new HttpRequestMessage(HttpMethod.Get, uri); - request.AddAuth(auth); - - HttpResponseMessage response = - await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) - .ConfigureAwait(false); - - return (await response - .GetResultAsync(cancellationToken) - .ConfigureAwait(false)) - .Result; + return client.GetAsync(uri, auth, cancellationToken); } } }