/// <summary> /// 交给实现了处理接口的子类处理 /// </summary> /// <param name="context">httpContext上下文</param> void Process(object context) { var httpContext = context as HttpListenerContext; var param = new DistributeParam(); try { param.context = httpContext; HttpDistributer.Distribute(param); param.response = new HttpResponse(httpContext); param.request = new HttpRequest(httpContext); param.httpHandler.Process(param.request, param.response, param.result); if (!string.IsNullOrEmpty(param.result.Result)) { param.response.Write(param.result.Result); } param.response.Flush(); param.response.Close(); } catch (Exception ex) { //异常日志 param.result.LogResult += ex.Message; param.result.Result = null; //记录日志 LogManager.GetCurrentClassLogger().Error(ex); } finally { //记录请求日志 param.WriteLog(); } }
/// <summary> /// 根据请求的路由找到具体的实现了的接口类 /// </summary> /// <param name="context"></param> /// <returns></returns> public static void Distribute(DistributeParam dsp) { var path = dsp.context.Request.Url.AbsolutePath; // /favicon.ico var code = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[0]; try { if (!string.IsNullOrEmpty(code)) { if (assembly == null) { assembly = Assembly.Load(GlobalData.assemblyName); } Type[] types = assembly.GetTypes(); foreach (Type type in types) { if (type.IsClass && !type.IsAbstract && typeof(IHttpHandler).IsAssignableFrom(type)) { var httpCode = type.GetCustomAttributes(typeof(RecvCodeAttribute), true).FirstOrDefault() as RecvCodeAttribute; if (httpCode != null && httpCode.Code.ToLower() == code.ToLower() && !httpCode.Disabled ) { dsp.httpCodeAtt = httpCode as RecvCodeAttribute; dsp.httpHandler = Activator.CreateInstance(type) as IHttpHandler; break; } } } } } catch { dsp.httpHandler = new DefaultHttpHandler(); } }