Exemplo n.º 1
0
        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 a common exception handler chain.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the common exception handler 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 Configure(this IExceptionPolicyBuilder builder, Action <IExceptionHandlerBuilder> configure)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.Post(configure));
 }
 /// <summary>
 /// Register a common exception handler chain.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the common exception handler chain is registered.</param>
 /// <param name="predicate">A filter used to determine whether the registered exception handler should be invoked.</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 Use(this IExceptionPolicyBuilder builder, Func <Exception, bool> predicate, Action <IExceptionHandlerBuilder> configure)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.Post(predicate, configure));
 }