/// <summary> /// Runs the middleware described by <see cref="TaskMiddlewareDescriptor"/>. /// </summary> /// <param name="descriptor">The task middleware to find and run.</param> /// <param name="context">The dispatch middleware context.</param> /// <param name="next">The next middleware callback.</param> /// <returns>A task that completes when the middleware has finished executing.</returns> public static Task RunAsync( TaskMiddlewareDescriptor descriptor, DispatchMiddlewareContext context, Func <Task> next) { Check.NotNull(descriptor, nameof(descriptor)); Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); IServiceProvider serviceProvider = context.GetProperty <IServiceProvider>(); ITaskMiddleware middleware = GetMiddleware(descriptor, serviceProvider); if (middleware is null) { // This _should not_ be possible, as TaskMiddlewareDescriptor is designed to only be constructable with // valid values. But a good sanity check here. ILogger logger = serviceProvider.CreateLogger(typeof(TaskMiddlewareRunner)); string message = Strings.MiddlewareCreationFailed(descriptor); logger.LogError(message); throw new InvalidOperationException(message); // TODO: maybe a better exception type. } return(middleware.InvokeAsync(context, next)); }
/// <summary> /// Add the middleware to the processing pipeline /// </summary> /// <param name="session">The current session</param> /// <param name="middleware">The middleware to add</param> /// <returns></returns> public static ProfilerSession AddMiddleware(this ProfilerSession session, ITaskMiddleware middleware) { session.ProcessingPipeline.SetNext(middleware); return(session); }