Пример #1
0
        public void ProcessRequest(HttpContext context)
        {
            string requestId = IncidentIdGenerator.GenerateIncidentId();

            log4net.ThreadContext.Properties["requestId"] = requestId;

            internalHttpHttpHandler = (IDetergentHttpHandler)context.Application[DetergentWebHttpHandlerId];

            if (internalHttpHttpHandler == null)
            {
                throw new ConfigurationErrorsException("DetergentWebHttpHandler does not have a configured inner handler");
            }

            WebHttpContext contextWrapper = new WebHttpContext(context);

            CurrentHttpContext.Current = contextWrapper;
            contextWrapper.SetRequestId(requestId);

            IHttpResponse response = internalHttpHttpHandler.ProcessRequest(contextWrapper);

            if (response != null)
            {
                response.Send(contextWrapper);
            }
        }
Пример #2
0
        private void WebRequestCallback(IAsyncResult result)
        {
            if (false == httpListener.IsListening)
            {
                return;
            }

            HttpListenerContext context = httpListener.EndGetContext(result);

            string requestId = IncidentIdGenerator.GenerateIncidentId();

            ThreadContext.Properties["requestId"] = requestId;

            httpListener.BeginGetContext(WebRequestCallback, httpListener);

            BufferedInputStream requestStream = new BufferedInputStream();

            requestStream.BufferStream(context.Request.InputStream);
            HttpListenerWrappedContext wrappedContext = new HttpListenerWrappedContext(
                context,
                requestStream,
                applicationRootUrl,
                applicationPath);

            CurrentHttpContext.Current = wrappedContext;
            wrappedContext.SetRequestId(requestId);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Received request: {0}", requestStream.DataToString(Encoding.UTF8));
            }

            IHttpResponse response = null;

            try
            {
                response = detergentHttpHandler.ProcessRequest(wrappedContext);
            }
            catch (Exception ex)
            {
                log.Error("Error processing the request", ex);
                response = new ExceptionStackHttpResponse(HttpStatusCode.InternalServerError, ex);
                response.Send(wrappedContext);
                return;
            }

            try
            {
                if (response != null)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Sending response type {0}", response.GetType().FullName);
                    }

                    response.Send(wrappedContext);
                }
                else if (log.IsDebugEnabled)
                {
                    log.Debug("Request has been processed without any response");
                }
            }
            catch (Exception ex)
            {
                log.Error("Could not send the response", ex);
                response = new ExceptionStackHttpResponse(HttpStatusCode.InternalServerError, ex);
                response.Send(wrappedContext);
            }
        }