private void GetResponseCallback(IAsyncResult result) { IAsyncExecutionContext executionContext = null; IHttpRequest <TRequestContent> httpRequest = null; try { executionContext = result.AsyncState as IAsyncExecutionContext; httpRequest = executionContext.RuntimeState as IHttpRequest <TRequestContent>; var httpResponse = httpRequest.EndGetResponse(result); executionContext.ResponseContext.HttpResponse = httpResponse; } catch (Exception exception) { httpRequest.Abort(); // Capture the exception and invoke outer handlers to // process the exception. executionContext.ResponseContext.AsyncResult.Exception = exception; } finally { executionContext.RequestContext.Metrics.StopEvent(Metric.HttpRequestTime); httpRequest.Dispose(); base.InvokeAsyncCallback(executionContext); } }
private void GetRequestStreamCallback(IAsyncResult result) { IAsyncExecutionContext executionContext = null; IHttpRequest <TRequestContent> httpRequest = null; try { executionContext = result.AsyncState as IAsyncExecutionContext; httpRequest = executionContext.RuntimeState as IHttpRequest <TRequestContent>; var requestContent = httpRequest.EndGetRequestContent(result); WriteContentToRequestBody(requestContent, httpRequest, executionContext.RequestContext); //var requestStream = httpRequest.EndSetRequestBody(result); httpRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), executionContext); } catch (Exception exception) { httpRequest.Abort(); httpRequest.Dispose(); // Capture the exception and invoke outer handlers to // process the exception. executionContext.ResponseContext.AsyncResult.Exception = exception; base.InvokeAsyncCallback(executionContext); } }
/// <summary> /// Issues an HTTP request for the current request context. /// </summary> /// <param name="executionContext">The execution context which contains both the /// requests and response context.</param> /// <returns>IAsyncResult which represent an async operation.</returns> public override IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext) { IHttpRequest <TRequestContent> httpRequest = null; try { SetMetrics(executionContext.RequestContext); httpRequest = CreateWebRequest(executionContext.RequestContext); executionContext.RuntimeState = httpRequest; IRequest wrappedRequest = executionContext.RequestContext.Request; if (executionContext.RequestContext.Retries == 0) { // First call, initialize an async result. executionContext.ResponseContext.AsyncResult = new RuntimeAsyncResult(executionContext.RequestContext.Callback, executionContext.RequestContext.State); } // Set request headers httpRequest.SetRequestHeaders(executionContext.RequestContext.Request.Headers); executionContext.RequestContext.Metrics.StartEvent(Metric.HttpRequestTime); if (wrappedRequest.HasRequestBody()) { // Send request body if present. httpRequest.BeginGetRequestContent(new AsyncCallback(GetRequestStreamCallback), executionContext); } else { // Get response if there is no response body to send. httpRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), executionContext); } return(executionContext.ResponseContext.AsyncResult); } catch (Exception exception) { if (executionContext.ResponseContext.AsyncResult != null) { // An exception will be thrown back to the calling code. // Dispose AsyncResult as it will not be used further. executionContext.ResponseContext.AsyncResult.Dispose(); executionContext.ResponseContext.AsyncResult = null; } if (httpRequest != null) { httpRequest.Abort(); httpRequest.Dispose(); } // Log this exception as it will not be caught by ErrorHandler. this.Logger.Error(exception, "An exception occured while initiating an asynchronous HTTP request."); throw; } }
/// <summary> /// Issues an HTTP request for the current request context. /// </summary> /// <typeparam name="T">The response type for the current request.</typeparam> /// <param name="executionContext">The execution context, it contains the /// request and response context.</param> /// <returns>A task that represents the asynchronous operation.</returns> public override async System.Threading.Tasks.Task <T> InvokeAsync <T>(IExecutionContext executionContext) { IHttpRequest <TRequestContent> httpRequest = null; try { SetMetrics(executionContext.RequestContext); IRequest wrappedRequest = executionContext.RequestContext.Request; httpRequest = CreateWebRequest(executionContext.RequestContext); httpRequest.SetRequestHeaders(wrappedRequest.Headers); using (executionContext.RequestContext.Metrics.StartEvent(Metric.HttpRequestTime)) { // Send request body if present. if (wrappedRequest.HasRequestBody()) { var requestContent = await httpRequest.GetRequestContentAsync().ConfigureAwait(false); WriteContentToRequestBody(requestContent, httpRequest, executionContext.RequestContext); } var response = await httpRequest.GetResponseAsync(executionContext.CancellationToken). ConfigureAwait(false); executionContext.ResponseContext.HttpResponse = response; } // The response is not unmarshalled yet. return(null); } catch (Exception) { if (httpRequest != null) { httpRequest.Abort(); } throw; } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Issues an HTTP request for the current request context. /// </summary> /// <param name="executionContext">The execution context which contains both the /// requests and response context.</param> public override void InvokeSync(IExecutionContext executionContext) { IHttpRequest <TRequestContent> httpRequest = null; try { SetMetrics(executionContext.RequestContext); IRequest wrappedRequest = executionContext.RequestContext.Request; httpRequest = CreateWebRequest(executionContext.RequestContext); httpRequest.SetRequestHeaders(wrappedRequest.Headers); using (executionContext.RequestContext.Metrics.StartEvent(Metric.HttpRequestTime)) { // Send request body if present. if (wrappedRequest.HasRequestBody()) { var requestContent = httpRequest.GetRequestContent(); WriteContentToRequestBody(requestContent, httpRequest, executionContext.RequestContext); } executionContext.ResponseContext.HttpResponse = httpRequest.GetResponse(); } } catch (Exception) { if (httpRequest != null) { httpRequest.Abort(); } throw; } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }