private static void BuildPolicy(IExceptionPolicyBuilder builder, PolicyConfiguration config, IDictionary <string, IExceptionFilter> filters) { foreach (var it in config.PreHandlers) { var handlerConfig = GetHandlerConfiguration(it.HandlerType); var filterableConfig = it as FilterableHandlerConfiguration; if (null == filterableConfig) { builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value))); } else { builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value))); } } foreach (var it in config.PostHandlers) { var handlerConfig = GetHandlerConfiguration(it.HandlerType); var filterableConfig = it as FilterableHandlerConfiguration; if (null == filterableConfig) { builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value))); } else { builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value))); } } foreach (var entry in config.PolicyEntries) { builder.For(entry.ExceptionType, entry.PostHandlingAction, handlerBuilder => { foreach (var it in entry.Handlers) { var handlerConfig = GetHandlerConfiguration(it.HandlerType); var filterableConfig = it as FilterableHandlerConfiguration; if (null == filterableConfig) { handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value)); } else { handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value)); } } }); } }
/// <summary> /// Register common exception handler chain which is invoked before the ones registered to exception type. /// </summary> /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the pre-hanlder-chain is registered.</param> /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param> /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns> public static IExceptionPolicyBuilder Pre(this IExceptionPolicyBuilder builder, Action <IExceptionHandlerBuilder> configure) { Guard.ArgumentNotNull(builder, nameof(builder)); return(builder.Pre(configure, _ => true)); }