Skip to content

Commit

Permalink
disposes response stream to close connection
Browse files Browse the repository at this point in the history
using .net core 2.1 on linux connection are stack on CLOSE_WAIT state
find out more @ https://github.com/dotnet/corefx/issues/37044
  • Loading branch information
pikos-apikos committed Aug 27, 2019
1 parent 5f14350 commit 0c62699
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/CheckoutSdk/ApiClient.cs
Expand Up @@ -76,17 +76,17 @@ public async Task<TResult> GetAsync<TResult>(string path, IApiCredentials creden
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
if (credentials == null) throw new ArgumentNullException(nameof(credentials));

var httpResponse = await SendRequestAsync(HttpMethod.Get, path, credentials, null, cancellationToken);
return await DeserializeJsonAsync<TResult>(httpResponse);
using(var httpResponse = await SendRequestAsync(HttpMethod.Get, path, credentials, null, cancellationToken))
return await DeserializeJsonAsync<TResult>(httpResponse);
}

public async Task<TResult> PostAsync<TResult>(string path, IApiCredentials credentials, CancellationToken cancellationToken, object request = null)
{
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
if (credentials == null) throw new ArgumentNullException(nameof(credentials));

var httpResponse = await SendRequestAsync(HttpMethod.Post, path, credentials, request, cancellationToken);
return await DeserializeJsonAsync<TResult>(httpResponse);
using(var httpResponse = await SendRequestAsync(HttpMethod.Post, path, credentials, request, cancellationToken))
return await DeserializeJsonAsync<TResult>(httpResponse);
}

public async Task<dynamic> PostAsync(string path, IApiCredentials credentials, Dictionary<HttpStatusCode, Type> resultTypeMappings, CancellationToken cancellationToken, object request = null)
Expand All @@ -95,12 +95,13 @@ public async Task<dynamic> PostAsync(string path, IApiCredentials credentials, D
if (credentials == null) throw new ArgumentNullException(nameof(credentials));
if (resultTypeMappings == null) throw new ArgumentNullException(nameof(resultTypeMappings));

var httpResponse = await SendRequestAsync(HttpMethod.Post, path, credentials, request, cancellationToken);

if (!resultTypeMappings.TryGetValue(httpResponse.StatusCode, out Type resultType))
throw new KeyNotFoundException($"The status code {httpResponse.StatusCode} is not mapped to a result type");
using(var httpResponse = await SendRequestAsync(HttpMethod.Post, path, credentials, request, cancellationToken))
{
if (!resultTypeMappings.TryGetValue(httpResponse.StatusCode, out Type resultType))
throw new KeyNotFoundException($"The status code {httpResponse.StatusCode} is not mapped to a result type");

return await DeserializeJsonAsync(httpResponse, resultType);
return await DeserializeJsonAsync(httpResponse, resultType);
}
}

private async Task<TResult> DeserializeJsonAsync<TResult>(HttpResponseMessage httpResponse)
Expand Down Expand Up @@ -170,4 +171,4 @@ private Uri GetRequestUri(string path)
return uri;
}
}
}
}

0 comments on commit 0c62699

Please sign in to comment.