protected override void ProcessRequest(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl)) return;

            var operationName = context.Request.GetOperationName();

            var httpReq = new ListenerRequest(operationName, context.Request);
            var httpRes = new ListenerResponse(context.Response);
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHttpHandler;
            if (serviceStackHandler != null)
            {
                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.Name;
                }
                serviceStackHandler.ProcessRequest(httpReq, httpRes, operationName);
                httpRes.Close();
                return;
            }

            throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);
        }
        protected override Task ProcessRequestAsync(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl)) 
                return ((object)null).AsTaskResult();

            var operationName = context.Request.GetOperationName();

            var httpReq = new ListenerRequest(operationName, context.Request);
            var httpRes = new ListenerResponse(context.Response);
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHttpHandler;
            if (serviceStackHandler != null)
            {
                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.Name;
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);                
                task.ContinueWith(x => httpRes.Close());

                return task;
            }

            return new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo)
                .AsTaskException();
        }
Ejemplo n.º 3
0
        public static void HandleError(Exception ex, HttpListenerContext context)
        {
            try
            {
                var errorResponse = new ErrorResponse
                {
                    ResponseStatus = new ResponseStatus
                    {
                        ErrorCode = ex.GetType().Name,
                        Message = ex.Message,
                        StackTrace = ex.StackTrace,
                    }
                };

                var operationName = context.Request.GetOperationName();
                var httpReq = new ListenerRequest(operationName, context.Request);
                var httpRes = new ListenerResponse(context.Response);
                var requestCtx = new HttpRequestContext(httpReq, httpRes, errorResponse);
                var contentType = requestCtx.ResponseContentType;

                var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
                if (serializer == null)
                {
                    contentType = HostContext.Config.DefaultContentType;
                    serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
                }

                httpRes.StatusCode = 500;
                httpRes.ContentType = contentType;

                serializer(requestCtx, errorResponse, httpRes);

                httpRes.Close();
            }
            catch (Exception errorEx)
            {
                var error = "Error this.ProcessRequest(context)(Exception while writing error to the response): [{0}]: {1}"
                            .Fmt(errorEx.GetType().Name, errorEx.Message);
                Log.ErrorFormat(error);
            }
        }