/// <summary> /// 添加策略方案 /// </summary> /// <param name="scheme"></param> public virtual void AddScheme(FilterPolicyBuilder scheme) { if (_schemes.ContainsKey(scheme.Name)) { throw new InvalidOperationException("策略方案名称已经存在: " + scheme.Name); } lock (_lock) { if (!TryAddScheme(scheme)) { throw new InvalidOperationException("策略方案名称已经存在: " + scheme.Name); } } }
/// <summary> /// 添加策略 /// </summary> /// <param name="name"></param> /// <param name="configureBuilder"></param> public void AddScheme(string name, Action <FilterPolicyBuilder> configureBuilder) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configureBuilder == null) { throw new ArgumentNullException(nameof(configureBuilder)); } if (SchemeMap.ContainsKey(name)) { throw new InvalidOperationException("策略名称已经存在:" + name); } var builder = new FilterPolicyBuilder(name); configureBuilder(builder); _schemes.Add(builder); SchemeMap[name] = builder; }
/// <summary> /// 尝试添加策略方案 /// </summary> /// <param name="scheme"></param> /// <returns></returns> public virtual bool TryAddScheme(FilterPolicyBuilder scheme) { if (_schemes.ContainsKey(scheme.Name)) { return(false); } lock (_lock) { if (_schemes.ContainsKey(scheme.Name)) { return(false); } if (typeof(IFilterPolicy).IsAssignableFrom(scheme.PolicyType)) { _requestHandlers.Add(scheme); _requestHandlersCopy = _requestHandlers.ToArray(); } _schemes[scheme.Name] = scheme; _schemesCopy = _schemes.Values.ToArray(); return(true); } }