From 7f95f55d47ef88edf22c055ed55ffda81872212a Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Fri, 22 May 2015 16:46:24 +0930 Subject: [PATCH] massaging and fighting with fxcop --- Octokit/Http/HttpClientAdapter.cs | 38 ++++++++++++++++++++++--------- Octokit/Http/IHttpClient.cs | 5 ++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Octokit/Http/HttpClientAdapter.cs b/Octokit/Http/HttpClientAdapter.cs index a17f5fb2..fd247c8d 100644 --- a/Octokit/Http/HttpClientAdapter.cs +++ b/Octokit/Http/HttpClientAdapter.cs @@ -18,21 +18,25 @@ namespace Octokit.Internal public class HttpClientAdapter : IHttpClient { readonly IWebProxy _webProxy; - HttpClient _http; + readonly HttpClient _http; public HttpClientAdapter() { } - public HttpClientAdapter(IWebProxy webProxy, HttpMessageHandler handler = null) + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] + public HttpClientAdapter(IWebProxy webProxy) { _webProxy = webProxy; + var handler = GetHandler(); + _http = new HttpClient(new RedirectHandler { InnerHandler = handler }); + } - if (handler == null) - { - handler = GetHandler(); - } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] + public HttpClientAdapter(IWebProxy webProxy, HttpMessageHandler handler) + { + Ensure.ArgumentNotNull(handler, "handler"); - _http = new HttpClient(new RedirectHandler() { InnerHandler = handler}); - + _webProxy = webProxy; + _http = new HttpClient(new RedirectHandler { InnerHandler = handler}); } /// @@ -71,8 +75,7 @@ namespace Octokit.Internal return cancellationTokenForRequest; } - - private HttpClientHandler GetHandler() + HttpClientHandler GetHandler() { var httpOptions = new HttpClientHandler { @@ -174,6 +177,20 @@ namespace Octokit.Internal } return null; } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (_http != null) _http.Dispose(); + } + } } public class RedirectHandler : DelegatingHandler @@ -182,7 +199,6 @@ namespace Octokit.Internal protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - var response = await base.SendAsync(request, cancellationToken); var allowAutoRedirect = (bool)request.Properties["AllowAutoRedirect"]; diff --git a/Octokit/Http/IHttpClient.cs b/Octokit/Http/IHttpClient.cs index 0818c525..f554d750 100644 --- a/Octokit/Http/IHttpClient.cs +++ b/Octokit/Http/IHttpClient.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System; +using System.Threading; using System.Threading.Tasks; namespace Octokit.Internal @@ -9,7 +10,7 @@ namespace Octokit.Internal /// /// Most folks won't ever need to swap this out. But if you're trying to run this on Windows Phone, you might. /// - public interface IHttpClient + public interface IHttpClient : IDisposable { /// /// Sends the specified request and returns a response.