public static void SetFilterOnService(List <Model.IFilter> filterList, Model.ServiceModel service) { List <IFilter> filterListCopy = filterList.ToList(); List <IFilter> methodFilter = new List <IFilter>(); methodFilter.AddRange(service.MethodInfo.DeclaringType.GetCustomAttributes <BaseFilter>(true)); methodFilter.AddRange(service.MethodInfo.GetCustomAttributes <BaseFilter>(true)); List <IFilter> noneExecFilter = methodFilter.FindAll(t => t is INoneExecuteFilter); //移除所有不执行标签 filterListCopy.RemoveAll(t => t is INoneExecuteFilter); foreach (var filter in noneExecFilter) { filterListCopy.RemoveAll(t => t.GetType().IsInstanceOfType(filter)); } service.FilterList = filterListCopy; }
public Stream Execute(string typeName, string functionName, Dictionary <string, object> args) { //执行方法 ServerResponse response = new ServerResponse(); Stopwatch watch = new Stopwatch(); Stopwatch allWatch = new Stopwatch(); ActionContext context = null; string sessionid = Guid.NewGuid().ToString(); OperationContext.Current.IncomingMessageProperties.Add(SessionIDKey, sessionid); string interfaceName = GetIntefaceName(typeName, functionName); try { #region 准备工作 ServiceModel service = GetServiceModel(interfaceName); MethodInfo method = null; if (service != null) { method = service.MethodInfo; } //如果找不到方法重新加载配置的DLL else { ServicePool.Instance.FillPool(); service = GetServiceModel(interfaceName); if (service != null) { method = service.MethodInfo; } } //如果再找不到方法,说明没有配置 if (method == null) { throw new Exception("未能找到接口:" + interfaceName + "!"); } context = new ActionContext(typeName, functionName, method, -1, args, null); ServiceSession session = new ServiceSession { Context = context, Method = method, Service = service.ServiceInfo, }; //MonitorCache.GetInstance().PushMessage(new CacheMessage { Message = interfaceName + " sessionid:" + sessionid }, SOAFramework.Library.CacheEnum.FormMonitor); _session.Add(sessionid, session); #endregion #region 执行前置filter IFilter failedFilter = ServiceUtility.FilterExecuting(service, context); if (failedFilter != null) { response.IsError = true; response.ErrorMessage = failedFilter.Message; response.Code = context.Code; ServiceUtility.FilterException(service, context); } #endregion #region 执行方法 if (!response.IsError) { try { watch.Start(); //执行方法 object result = service.Invoke(args); watch.Stop(); response.Data = result; } catch { ServiceUtility.FilterException(service, context); throw; } WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; } #endregion #region 执行后置filter context.PerformanceContext.ElapsedMilliseconds = watch.ElapsedMilliseconds; context.Response = response; _session[sessionid] = session; failedFilter = ServiceUtility.FilterExecuted(service, response, context); if (failedFilter != null && !response.IsError) { response.IsError = true; response.ErrorMessage = failedFilter.Message; response.Code = context.Code; ServiceUtility.FilterException(service, context); } #endregion } catch (Exception ex) { Exception exinner = ex; while (exinner.InnerException != null) { exinner = exinner.InnerException; } response.IsError = true; response.ErrorMessage = exinner.Message; response.StackTrace = exinner.StackTrace; } #region 处理结果 Stream stream = response.ToStream(enableZippedResponse); allWatch.Stop(); if (EnableConsoleMonitor) { Console.WriteLine("{0} -- 耗时:{1}", interfaceName, allWatch.ElapsedMilliseconds); } if (_session.ContainsKey(sessionid)) { _session[sessionid].Dispose(); _session.Remove(sessionid); } #endregion return(stream); }