/// <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> /// <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 { 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); 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) { 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.Dispose(); } throw; } }
public override IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext) { IHttpRequest <TRequestContent> httpRequest = null; try { SetMetrics(executionContext.RequestContext); httpRequest = (IHttpRequest <TRequestContent>)(executionContext.RuntimeState = CreateWebRequest(executionContext.RequestContext)); IRequest request = executionContext.RequestContext.Request; if (executionContext.RequestContext.Retries == 0) { executionContext.ResponseContext.AsyncResult = new RuntimeAsyncResult(executionContext.RequestContext.Callback, executionContext.RequestContext.State); executionContext.ResponseContext.AsyncResult.AsyncOptions = executionContext.RequestContext.AsyncOptions; executionContext.ResponseContext.AsyncResult.Action = executionContext.RequestContext.Action; executionContext.ResponseContext.AsyncResult.Request = executionContext.RequestContext.OriginalRequest; } httpRequest.SetRequestHeaders(executionContext.RequestContext.Request.Headers); executionContext.RequestContext.Metrics.StartEvent(Metric.HttpRequestTime); if (request.HasRequestBody()) { httpRequest.BeginGetRequestContent(GetRequestStreamCallback, executionContext); } else { httpRequest.BeginGetResponse(GetResponseCallback, executionContext); } return(executionContext.ResponseContext.AsyncResult); } catch (Exception exception) { if (executionContext.ResponseContext.AsyncResult != null) { executionContext.ResponseContext.AsyncResult.Dispose(); executionContext.ResponseContext.AsyncResult = null; } httpRequest?.Dispose(); Logger.Error(exception, "An exception occured while initiating an asynchronous HTTP request."); throw; } }