コード例 #1
0
        protected override void ProcessRequest(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl))
            {
                return;
            }

            var operationName = context.Request.GetOperationName();

            var httpReq = new HttpListenerRequestWrapper(operationName, context.Request);
            var httpRes = new HttpListenerResponseWrapper(context.Response);
            var handler = ServiceStackHttpHandlerFactory.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);
        }
コード例 #2
0
        // Handle the processing of a request in here.
        private void ListenerCallback(IAsyncResult asyncResult)
        {
            var listener = asyncResult.AsyncState as HttpListener;
            HttpListenerContext context = null;

            if (listener == null)
            {
                return;
            }

            try
            {
                if (!IsListening)
                {
                    Log.DebugFormat("Ignoring ListenerCallback() as HttpListener is no longer listening");
                    return;
                }
                // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework,
                // blocks until there is a request to be processed or some type of data is available.
                context = listener.EndGetContext(asyncResult);
            }
            catch (Exception ex)
            {
                // You will get an exception when httpListener.Stop() is called
                // because there will be a thread stopped waiting on the .EndGetContext()
                // method, and again, that is just the way most Begin/End asynchronous
                // methods of the .NET Framework work.
                var errMsg = ex + ": " + IsListening;
                Log.Warn(errMsg);
                return;
            }
            finally
            {
                // Once we know we have a request (or exception), we signal the other thread
                // so that it calls the BeginGetContext() (or possibly exits if we're not
                // listening any more) method to start handling the next incoming request
                // while we continue to process this request on a different thread.
                ListenForNextRequest.Set();
            }

            if (context == null)
            {
                return;
            }

            Log.InfoFormat("{0} Request : {1}", context.Request.UserHostAddress, context.Request.RawUrl);

            //System.Diagnostics.Debug.WriteLine("Start: " + requestNumber + " at " + DateTime.UtcNow);
            //var request = context.Request;

            //if (request.HasEntityBody)

            RaiseReceiveWebRequest(context);

            try
            {
                this.ProcessRequest(context);
            }
            catch (Exception ex)
            {
                var error = string.Format("Error this.ProcessRequest(context): [{0}]: {1}", ex.GetType().Name, ex.Message);
                Log.ErrorFormat(error);

                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 HttpListenerRequestWrapper(operationName, context.Request);
                    var httpRes       = new HttpListenerResponseWrapper(context.Response);
                    var requestCtx    = new HttpRequestContext(httpReq, httpRes, errorResponse);
                    var contentType   = requestCtx.ResponseContentType;

                    var serializer = EndpointHost.ContentTypeFilter.GetResponseSerializer(contentType);
                    if (serializer == null)
                    {
                        contentType = EndpointHost.Config.DefaultContentType;
                        serializer  = EndpointHost.ContentTypeFilter.GetResponseSerializer(contentType);
                    }

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

                    serializer(requestCtx, errorResponse, httpRes);

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

            //System.Diagnostics.Debug.WriteLine("End: " + requestNumber + " at " + DateTime.UtcNow);
        }
コード例 #3
0
        protected override void ProcessRequest(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl))
            {
                return;
            }

            var operationName = context.Request.GetOperationName();

            var    httpReq = new HttpListenerRequestWrapper(operationName, context.Request);
            string pathInfo;
            string servicePath;

            AntServiceStackHttpHandlerFactory.GetServicePathInfo(httpReq.PathInfo, out servicePath, out pathInfo);
            httpReq.SetServicePath(servicePath);
            var httpRes = new HttpListenerResponseWrapper(context.Response);

            HostContext.InitRequest(httpReq, httpRes);

            var handler             = AntServiceStackHttpHandlerFactory.GetHandler(httpReq);
            var serviceStackHandler = handler as IServiceStackHttpHandler;

            if (serviceStackHandler != null)
            {
                var endpointHandler = serviceStackHandler as EndpointHandlerBase;
                if (endpointHandler != null)
                {
                    httpReq.OperationName = operationName = endpointHandler.RequestName;
                    if (!string.IsNullOrWhiteSpace(operationName))
                    {
                        bool isAsync = EndpointHost.MetadataMap[endpointHandler.ServicePath.ToLower()].OperationNameMap[operationName.ToLower()].IsAsync;
                        if (isAsync)
                        {
                            var task = endpointHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                            task.ContinueWith(t =>
                            {
                                try
                                {
                                    if (t.Exception != null)
                                    {
                                        log.Error("Error happened in async service execution!", t.Exception.InnerException ?? t.Exception,
                                                  new Dictionary <string, string>()
                                        {
                                            { "ErrorCode", "FXD300079" }, { "HostMode", "Self-Host" }
                                        });
                                    }
                                    httpRes.Close();
                                }
                                catch {  }
                            });
                            return;
                        }
                    }
                }

                serviceStackHandler.ProcessRequest(httpReq, httpRes, operationName);
                httpRes.Close();
                return;
            }

            throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);
        }