private ApiResult <T> CreateHttpRequestExceptionResult <T>(HttpRequestException ex) { Debug.WriteLine($"Error in {nameof(ApiService)}: {ex.ToString()}"); _loggingService.WriteError(ex); return(new ApiResult <T> { IsSuccess = false, StatusCode = System.Net.HttpStatusCode.RequestTimeout }); }
public async Task SendAsync_GetUsingChunkedEncoding_ThrowsHttpRequestException() { // WinHTTP doesn't support GET requests with a request body that uses // chunked encoding. This test pins this behavior and verifies that the // error handling is working correctly. var server = new Uri("http://www.microsoft.com"); // No network I/O actually happens. var request = new HttpRequestMessage(HttpMethod.Get, server); request.Content = new StringContent("Request body"); request.Headers.TransferEncodingChunked = true; var handler = new WinHttpHandler(); using (HttpClient client = new HttpClient(handler)) { HttpRequestException ex = await Assert.ThrowsAsync <HttpRequestException>(() => client.SendAsync(request)); _output.WriteLine(ex.ToString()); } }
public async Task <CompilerResponse> GetCompilationResponse(string script, string entityType, IDictionary <string, string> references, string requestId = "") { DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : Waiting on semaphore ..."); await _semaphoreObject.WaitAsync(); DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : Entered critical section ..."); try { // If for any reason, compiler host is not running due to failures in process monitor, launch it before making a call. if (!_isComplierHostRunning) { await LaunchCompilerHostProcess(requestId); } HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_compilerHostUrl}/api/compilerhost") { Content = new StringContent(JsonConvert.SerializeObject(PrepareRequestBody(script, entityType, references)), Encoding.UTF8, "application/json") }; if (!string.IsNullOrWhiteSpace(requestId)) { requestMessage.Headers.Add(HeaderConstants.RequestIdHeaderName, requestId); } HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage); if (!responseMessage.IsSuccessStatusCode) { string errorResponse = await responseMessage.Content.ReadAsStringAsync(); HttpRequestException ex = new HttpRequestException($"Status Code : {responseMessage.StatusCode}, Content : {errorResponse}"); DiagnosticsETWProvider.Instance.LogCompilerHostClientException(requestId, _eventSource, string.Empty, ex.GetType().ToString(), ex.ToString()); throw ex; } return(await responseMessage.Content.ReadAsAsyncCustom <CompilerResponse>()); } finally { _semaphoreObject.Release(); DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : semaphore released."); } }
private async Task <bool> CheckForHealthPing(string requestId) { HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{_compilerHostUrl}/api/compilerhost/healthping"); if (!string.IsNullOrWhiteSpace(requestId)) { requestMessage.Headers.Add(HeaderConstants.RequestIdHeaderName, requestId); } HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage); if (!responseMessage.IsSuccessStatusCode) { string errorResponse = await responseMessage.Content.ReadAsStringAsync(); HttpRequestException ex = new HttpRequestException($"Status Code : {responseMessage.StatusCode}, Content : {errorResponse}"); DiagnosticsETWProvider.Instance.LogCompilerHostClientWarning(requestId, _eventSource, "Compiler host health ping failed", ex.GetType().ToString(), ex.ToString()); throw ex; } return(true); }