Beispiel #1
0
        private void GetRequestStreamCallbackHelper(object state)
        {
            IAsyncResult result = state as IAsyncResult;

            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.Dispose();

                // Capture the exception and invoke outer handlers to
                // process the exception.
                executionContext.ResponseContext.AsyncResult.Exception = exception;
                base.InvokeAsyncCallback(executionContext);
            }
        }
Beispiel #2
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);
                }

                // 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;
            }
        }
Beispiel #3
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
            {
                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;
            }
        }
Beispiel #4
0
        private RestOperation PerformOperationAsync(string operationName, IProcessResponses responseProcessor, params object[] args)
        {
            JsonObject argsObject = null;

            if ((args != null) && (args.Length != 0))
            {
                argsObject = (JsonObject)args[0];
            }

            var operation = new RestOperation();

            IHttpRequest webRequest = _requestBuilder.CreateRequest(operationName, argsObject);

            webRequest.BeginGetResponse(ar => InterpretResponse(responseProcessor, operation, () => webRequest.EndGetResponse(ar)), null);

            return(operation);
        }
Beispiel #5
0
        private void GetRequestStreamCallbackHelper(object state)
        {
            IAsyncResult                   asyncResult           = state as IAsyncResult;
            IAsyncExecutionContext         asyncExecutionContext = null;
            IHttpRequest <TRequestContent> httpRequest           = null;

            try
            {
                asyncExecutionContext = (asyncResult.AsyncState as IAsyncExecutionContext);
                httpRequest           = (asyncExecutionContext.RuntimeState as IHttpRequest <TRequestContent>);
                TRequestContent requestContent = httpRequest.EndGetRequestContent(asyncResult);
                WriteContentToRequestBody(requestContent, httpRequest, asyncExecutionContext.RequestContext);
                httpRequest.BeginGetResponse(GetResponseCallback, asyncExecutionContext);
            }
            catch (Exception exception)
            {
                httpRequest.Dispose();
                asyncExecutionContext.ResponseContext.AsyncResult.Exception = exception;
                base.InvokeAsyncCallback(asyncExecutionContext);
            }
        }
Beispiel #6
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;
            }
        }