Example #1
0
        public override void InvokeSync(IExecutionContext executionContext)
        {
            IHttpRequest <TRequestContent> httpRequest = null;

            try
            {
                SetMetrics(executionContext.RequestContext);
                IRequest request = executionContext.RequestContext.Request;
                httpRequest = CreateWebRequest(executionContext.RequestContext);
                httpRequest.SetRequestHeaders(request.Headers);
                using (executionContext.RequestContext.Metrics.StartEvent(Metric.HttpRequestTime))
                {
                    if (request.HasRequestBody())
                    {
                        try
                        {
                            TRequestContent requestContent = httpRequest.GetRequestContent();
                            WriteContentToRequestBody(requestContent, httpRequest, executionContext.RequestContext);
                        }
                        catch
                        {
                            CompleteFailedRequest(httpRequest);
                            throw;
                        }
                    }
                    executionContext.ResponseContext.HttpResponse = httpRequest.GetResponse();
                }
            }
            finally
            {
                httpRequest?.Dispose();
            }
        }
Example #2
0
        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;
            }
        }
 public void Dispose()
 {
     req.Dispose();
 }
Example #4
0
 public void TestRequestDisposeNotImplemented()
 {
     req.Dispose();
 }
Example #5
0
        /// <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);

#if UNITY
                    executionContext.ResponseContext.AsyncResult.AsyncOptions = executionContext.RequestContext.AsyncOptions;
                    executionContext.ResponseContext.AsyncResult.Action       = executionContext.RequestContext.Action;
                    executionContext.ResponseContext.AsyncResult.Request      = executionContext.RequestContext.OriginalRequest;
#endif
                }

                // 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.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;
            }
        }