/// <summary> /// Initializes a new instance of <c>ExceptionHandlerConfigurator</c> class. /// </summary> /// <param name="context">The configuration context.</param> /// <param name="exceptionType">The type of the exception to be handled.</param> /// <param name="exceptionHandlerType">The type of the exception handler.</param> /// <param name="behavior">The exception handling behavior.</param> public ExceptionHandlerConfigurator(IConfigSourceConfigurator context, Type exceptionType, Type exceptionHandlerType, ExceptionHandlingBehavior behavior) : base(context) { this.exceptionType = exceptionType; this.exceptionHandlerType = exceptionHandlerType; this.behavior = behavior; }
private void RegisterHandlerOrig(Type exceptionType, ExceptionHandlingBehavior behavior, IExceptionHandler handler) { if (handlersOrig.ContainsKey(exceptionType)) { var exceptionConfigItem = handlersOrig[exceptionType]; var list = exceptionConfigItem.Handlers; if (!list.Contains(handler, new ExceptionHandlerComparer())) { list.Add(handler); } } else { ExceptionConfigItem configItem = new ExceptionConfigItem(); configItem.Behavior = behavior; configItem.Handlers.Add(handler); handlersOrig[exceptionType] = configItem; } }
/// <summary> /// Adds an exception definition to the ConfigSource. /// </summary> /// <param name="exceptionType">The type of the exception to be guarded.</param> /// <param name="behavior">The exception handling behavior.</param> public void AddException(Type exceptionType, ExceptionHandlingBehavior behavior = ExceptionHandlingBehavior.Direct) { if (this.config.Exceptions == null) { this.config.Exceptions = new ExceptionElementCollection(); } foreach (ExceptionElement ee in this.config.Exceptions) { if (ee.Type == exceptionType.AssemblyQualifiedName && ee.Behavior == behavior) { return; } } this.config.Exceptions.Add(new ExceptionElement { Type = exceptionType.AssemblyQualifiedName, Behavior = behavior, Handlers = new ExceptionHandlerElementCollection() }); }
private ExceptionManager() { try { OFoodsConfigSection config = AppRuntime.Instance.CurrentApplication.ConfigSource.Config; if (config.Exceptions != null && config.Exceptions.Count > 0) { ExceptionElementCollection exceptionElementCollection = config.Exceptions; foreach (ExceptionElement exceptionElement in exceptionElementCollection) { Type exceptionType = Type.GetType(exceptionElement.Type); if (exceptionType == null) { continue; } if (exceptionType.IsAbstract || !typeof(Exception).IsAssignableFrom(exceptionType)) { continue; } ExceptionHandlingBehavior handlingBehavior = exceptionElement.Behavior; if (exceptionElement.Handlers != null && exceptionElement.Handlers.Count > 0) { foreach (ExceptionHandlerElement exceptionHandlerElement in exceptionElement.Handlers) { Type handlerType = Type.GetType(exceptionHandlerElement.Type); if (handlerType != null) { if (handlerType.IsAbstract || !handlerType.GetInterfaces().Any(p => p.Equals(typeof(IExceptionHandler)))) { continue; } try { IExceptionHandler exceptionHandler = (IExceptionHandler)Activator.CreateInstance(handlerType); this.RegisterHandlerOrig(exceptionType, handlingBehavior, exceptionHandler); } catch { continue; } // try } // if } // foreach - exception handler } else { handlersOrig.Add(exceptionType, new ExceptionConfigItem { Behavior = handlingBehavior, Handlers = new List <IExceptionHandler>() }); } } // foreach - exception BuildResponsibilityChain(); } // if } catch { } }
/// <summary> /// Adds an exception handler to the Apworks framework. /// </summary> /// <typeparam name="TException">The type of the exception to be handled.</typeparam> /// <typeparam name="TExceptionHandler">The type of the exception handler.</typeparam> /// <param name="configurator">The <see cref="IExceptionHandlerConfigurator"/> to be extended.</param> /// <param name="behavior">The exception handling behavior.</param> /// <returns>The <see cref="IExceptionHandlerConfigurator"/> instance.</returns> public static IExceptionHandlerConfigurator AddExceptionHandler <TException, TExceptionHandler>(this IExceptionHandlerConfigurator configurator, ExceptionHandlingBehavior behavior = ExceptionHandlingBehavior.Direct) where TException : Exception where TExceptionHandler : IExceptionHandler { return(new ExceptionHandlerConfigurator(configurator, typeof(TException), typeof(TExceptionHandler), behavior)); }
/// <summary> /// Adds an exception definition to the ConfigSource. /// </summary> /// <param name="exceptionType">The type of the exception to be guarded.</param> /// <param name="behavior">The exception handling behavior.</param> public void AddException(Type exceptionType, ExceptionHandlingBehavior behavior = ExceptionHandlingBehavior.Direct) { if (this.config.Exceptions == null) this.config.Exceptions = new ExceptionElementCollection(); foreach (ExceptionElement ee in this.config.Exceptions) { if (ee.Type == exceptionType.AssemblyQualifiedName && ee.Behavior == behavior) return; } this.config.Exceptions.Add(new ExceptionElement { Type = exceptionType.AssemblyQualifiedName, Behavior = behavior, Handlers = new ExceptionHandlerElementCollection() }); }
/// <summary> /// Adds an exception definition to the ConfigSource. /// </summary> /// <param name="exceptionType">The type of the exception to be guarded.</param> /// <param name="behavior">The exception handling behavior.</param> public void AddException(Type exceptionType, ExceptionHandlingBehavior behavior = ExceptionHandlingBehavior.Direct) { this.config.Exceptions.Add(new ExceptionElement { Type = exceptionType.AssemblyQualifiedName, Behavior = behavior, Handlers = new ExceptionHandlerElementCollection() }); }