Пример #1
0
        /// <summary>
        /// Executes the MiniProfiler-wrapped middleware.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
        /// <returns>A task that represents the execution of the MiniProfiler-wrapped middleware.</returns>
        /// <exception cref="ArgumentNullException">Throws when <paramref name="context"/> is <c>null</c>.</exception>
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Request.Path.StartsWithSegments(MatchPath, out PathString subPath))
            {
                // This is a request in the MiniProfiler path (e.g. one of "our" routes), HANDLE THE SITUATION.
                await HandleRequest(context, subPath).ConfigureAwait(false);

                return;
            }

            // Otherwise this is an app request, profile it!
            if (Options.ShouldProfile?.Invoke(context.Request) ?? true)
            {
                // Wrap the request in this profiler
                var mp = MiniProfiler.Start();
                // Always add this profiler's header (and any async requests before it)
                using (mp.Step("MiniProfiler Prep"))
                {
                    await SetHeadersAndState(context, mp).ConfigureAwait(false);
                }
                // Execute the pipe
#pragma warning disable RCS1090 // Call 'ConfigureAwait(false)'.
                await _next(context);

#pragma warning restore RCS1090 // Call 'ConfigureAwait(false)'.
                // Stop (and record)
                await MiniProfiler.StopAsync().ConfigureAwait(false);
            }
            else
            {
                // Don't profile, only relay
#pragma warning disable RCS1090 // Call 'ConfigureAwait(false)'.
                await _next(context);

#pragma warning restore RCS1090 // Call 'ConfigureAwait(false)'.
            }
        }