private INextHandler BuildNextHandler( IReadOnlyCollection <Type> delegatingHandlerTypes, IReadOnlyCollection <Type> handlerTypes ) { return(new NextHandlerRunner(async message => { if (message == null) { throw new NullMessageTypeException(); } if (!delegatingHandlerTypes.Any()) { updater.PushMarker(); var result = await RunHandlers(handlerTypes, message); updater.PopMarker(); return result; } var head = delegatingHandlerTypes.First(); var tail = delegatingHandlerTypes.Skip(1).ToList(); var nextHandler = (IDelegatingHandler)dependencyScope.GetService(head); var nextFunction = BuildNextHandler(tail, handlerTypes); return await nextHandler.Handle(nextFunction, message); })); }
private INextHandler BuildNextHandler( IReadOnlyCollection <Type> delegatingHandlerTypes, IReadOnlyCollection <Type> handlerTypes, CancellationToken cancellation) { return(new NextHandlerRunner(async message => { if (message == null) { throw new NullMessageTypeException(); } if (!delegatingHandlerTypes.Any()) { updater.PushMarker(); var result = await RunHandlers(handlerTypes, message, cancellation); updater.PopMarker(); return result; } var head = delegatingHandlerTypes.First(); var tail = delegatingHandlerTypes.Skip(1).ToList(); var nextFunction = BuildNextHandler(tail, handlerTypes, cancellation); var pipelineHanlder = dependencyScope.GetService(head); if (pipelineHanlder is IDelegatingHandler) { var nextHandler = pipelineHanlder as IDelegatingHandler; return await nextHandler.Handle(nextFunction, message); } else if (pipelineHanlder is ICancelableDelegatingHandler) { var nextHandler = pipelineHanlder as ICancelableDelegatingHandler; return await nextHandler.Handle(nextFunction, message, cancellation); } else { throw new AskedForDelegatingHandlerButDidNotGetADelegatingHandlerException(); } })); }