public virtual async Task <ApiResponse> SendAsync(HttpMethod method, string resource, HttpContent content = null) { // Create the context here so we have access to it in the catch block Polly.Context context = new Polly.Context(); //Create the Request HttpRequestMessage request = new HttpRequestMessage(method, resource); if (content != null) { request.Content = content; } // Set the PolicyExecutionContext so that it is available after execution of the request // https://github.com/App-vNext/Polly/issues/505 request.SetPolicyExecutionContext(context); AugmentContext(context, resource, request); // Make the request RequestCounter++; LastRequestTimeStamp = DateTime.UtcNow; try { _logger.LogDebug($"{DateTime.Now.ToString()} : Sending request with Method: {method.ToString()} to Resource: {resource}"); using (var response = await _client.SendAsync(request, CancellationTokenSource.Token)) { TransferRetryInfo(response.RequestMessage, context); return(await _apiResponseBuilder.GetApiResponse(response, resource)); } } catch (Exception exception) { // Handles communication errors such as "Connection Refused" etc. // Network failures (System.Net.Http.HttpRequestException) // Timeouts (System.IO.IOException) AugmentException(exception, resource, request); TransferRetryInfo(exception, context); return(_apiResponseBuilder.GetApiResponse(exception, request, resource)); } }
// Send HttpContent, used by all above methods, accept any of the HttpContent sub-classes // FormUrlEncodedContent: A container for name/value tuples encoded using application/x-www-form-urlencoded MIME type. // MultipartContent: Provides a collection of HttpContent objects that get serialized using the multipart/* content type specification. // MultipartFormDataContent: Provides a container for content encoded using multipart/form-data MIME type. (Use this format if you are uploading a file to a server) // StreamContent: Provides HTTP content based on a stream. // StringContent: Provides HTTP content based on a string. // ObjectContent: Contains a value as well as an associated MediaTypeFormatter that will be used to serialize the value when writing this content. // ObjectContent comes from the System.Net.Http.Formatting assembly provided by package Microsoft.AspNet.WebApi.Client // HttpContent: A base class representing an HTTP entity body and content headers. public virtual async Task <ApiResponse> SendAsync(HttpMethod method, string resourcePath, HttpContent content = null) { // Create the context here so we have access to it in the catch block Polly.Context context = new Polly.Context(); //Create the Request HttpRequestMessage request = new HttpRequestMessage(method, resourcePath); if (content != null) { request.Content = content; } else { // content is normally provided for post and put methods if (method == HttpMethod.Post || method == HttpMethod.Put) { _logger.LogDebug($"{DateTime.Now.ToString()} : SendAsync: The HttpContent is null for POST or PUT request!"); } } // Set the PolicyExecutionContext so that it is available after execution of the request // https://github.com/App-vNext/Polly/issues/505 request.SetPolicyExecutionContext(context); request.SetResourcePath(resourcePath); // Make the request RequestCount++; PendingRequestCount++; LastRequestTimeStamp = DateTime.UtcNow; var requestTimer = new Stopwatch(); try { if (content == null) { _logger.LogDebug($"{DateTime.Now.ToString()} : SendAsync: Sending request with Method: {method?.ToString()} HttpContent is null, RequestUri: {request.RequestUri}"); } else { _logger.LogDebug($"{DateTime.Now.ToString()} : SendAsync: Sending request with Method: {method?.ToString()} HttpContent Type: \"{content?.GetType()?.Name?.ToString()}\" RequestUri: {request.RequestUri}"); } requestTimer.Start(); using (var response = await _client.SendAsync(request, CancellationTokenSource.Token)) { requestTimer.Stop(); TransferRetryInfo(response.RequestMessage, context); return(await _apiResponseBuilder.GetApiResponse(response, resourcePath, requestTimer)); } } catch (Exception exception) { // Handles communication errors such as "Connection Refused" etc. // Network failures (System.Net.Http.HttpRequestException) // Timeouts (System.IO.IOException) requestTimer.Stop(); TransferRetryInfo(exception, context); return(_apiResponseBuilder.GetApiResponseForException(exception, request, resourcePath, requestTimer)); } finally { PendingRequestCount--; } }