private void WithHandler(HandlerType type, HandlerPriority priority, TypedHandlerInfo handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } lock (_handlers) { if (!_handlers.ContainsKey(type)) { _handlers[type] = new Dictionary <HandlerPriority, ICollection <TypedHandlerInfo> >() { { priority, new List <TypedHandlerInfo> { handler } } }; } else { var handlersForType = _handlers[type]; if (!handlersForType.ContainsKey(priority)) { handlersForType[priority] = new List <TypedHandlerInfo> { handler }; } else { handlersForType[priority].Add(handler); } } } }
public TypedHandlerRegister WithAsyncExceptionHandler(HandlerPriority priority, Func <TypedExceptionContext, Task> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } var handlerInfo = new TypedHandlerInfo { Handler = context => handler((TypedExceptionContext)context), InitialConstructor = GetOrAddFromCtorCache(HandlerType.Exception, handler.GetType(), false, (Func <ITypedBuilderContext, HttpRequestMessage, HttpResponseMessage, Exception, TypedExceptionContext>)((ctx, request, response, exception) => new TypedExceptionContext(ctx, request, response, exception))), ContinuationConstructor = GetOrAddFromCtorCache(HandlerType.Exception, handler.GetType(), true, (Func <TypedExceptionContext, TypedExceptionContext>)(ctx => new TypedExceptionContext(ctx))) }; WithHandler(HandlerType.Exception, priority, handlerInfo); return(this); }
public TypedHandlerRegister WithAsyncErrorHandler <TError>(HandlerPriority priority, Func <TypedErrorContext <TError>, Task> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } var handlerInfo = new TypedHandlerInfo { Handler = context => handler((TypedErrorContext <TError>)context), InitialConstructor = GetOrAddFromCtorCache(HandlerType.Error, handler.GetType(), false, (Func <ITypedBuilderContext, HttpRequestMessage, HttpResponseMessage, object, TypedErrorContext>)((ctx, request, response, error) => new TypedErrorContext <TError>(ctx, request, response, TypeHelpers.CheckType <TError>(error, ctx.SuppressTypeMismatchExceptions)))), ContinuationConstructor = GetOrAddFromCtorCache(HandlerType.Error, handler.GetType(), true, (Func <TypedErrorContext, TypedErrorContext>)(ctx => new TypedErrorContext <TError>(ctx))), }; WithHandler(HandlerType.Error, priority, handlerInfo); return(this); }