internal static IEnumerable <HttpRequestHandlerBase> LoadControllers(Type Module, string controllerNamespace)
        {
            List <HttpRequestHandlerBase> ret = new List <HttpRequestHandlerBase>();

            if (Module != null && !string.IsNullOrWhiteSpace(controllerNamespace))
            {
                IEnumerable <Type> controllers = Module.Assembly.GetTypes().Where(type => (type.FullName.StartsWith(controllerNamespace) && type.IsSubclassOf(typeof(HttpRequestHandlerBase))));
                foreach (Type type in controllers)
                {
                    object instance = type.Assembly.CreateInstance(type.FullName);
                    HttpRequestHandlerBase action = instance as HttpRequestHandlerBase;
                    if (action != null)
                    {
                        ret.Add(action);
                    }
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 添加一个路由及请求处理器
 /// </summary>
 /// <param name="handler"></param>
 public void AddPath(HttpRequestHandlerBase handler)
 {
     PathDictionary.Add(handler.Path, handler);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 用户发起请求后的回调
        /// </summary>
        /// <param name="result">HttpListener</param>
        private void ListenerCallback(IAsyncResult result)
        {
            HttpListener        listener = (HttpListener)result.AsyncState;
            HttpListenerContext context  = null;

            try
            {
                //尝试读取用户请求
                context = listener.EndGetContext(result);
            }
            catch (Exception ex)
            {
                LogData(ex.Message);
            }
            if (listener.IsListening)
            {
                listener.BeginGetContext(ListenerCallback, listener);
            }
            if (context != null)
            {
                //读取用户请求成功,放入线程池中执行
                ThreadPool.QueueUserWorkItem(h =>
                {
                    HttpListenerRequest request   = context.Request;  //用户请求
                    HttpListenerResponse response = context.Response; //请求响应结果

                    //获取用户请求路径
                    string requestPath = request.Url.AbsolutePath;
                    if (!requestPath.EndsWith("/"))
                    {
                        requestPath += "/";
                    }
                    LogData(string.Format("http request path: {0}, Method: {1}", requestPath, request.HttpMethod));

                    //对请求路径进行匹配
                    HttpRequestHandlerBase requestHandler = null;
                    if (pathDictionary.TryGetValue(requestPath, out requestHandler) && requestHandler != null)
                    {
                        //路径匹配成功并且成功找到请求处理器
                        try
                        {
                            //开始处理请求
                            requestHandler.Handler(request, response);
                        }
                        catch (Exception ex)
                        {
                            //处理请求时出错
                            LogData(ex.Message);
                            //返回HTTP 500错误
                            Default500(response);
                        }
                    }
                    else
                    {
                        //无法匹配请求处理器
                        //返回HTTP 404错误
                        Default404(response);
                    }
                    //关闭输出流
                    Stream output = response.OutputStream;
                    output.Close();
                });
            }
        }