示例#1
0
        /// <summary>
        /// Called asynchronously before the action, after model binding is complete.
        /// </summary>
        /// <param name="context">The <see cref="ActionExecutingContext" />.</param>
        /// <param name="next">The <see cref="ActionExecutionDelegate" />. Invoked to execute the next action filter or the action itself.</param>
        /// <returns>A <see cref="Task" /> that on completion indicates the filter has executed.</returns>
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            await AspNetCoreInfrastructure.InvokeUserAgentSentinelAsync(context.HttpContext, Options, (message, response) =>
            {
                response.StatusCode = (int)message.StatusCode;
                response.Headers.AddOrUpdateHeaders(message.Headers);
            }).ContinueWithSuppressedContext();

            await next().ContinueWithSuppressedContext();
        }
        /// <summary>
        /// Executes the <see cref="ThrottlingSentinelMiddleware" />.
        /// </summary>
        /// <param name="context">The context of the current request.</param>
        /// <param name="tc">The dependency injected <see cref="IThrottlingCache"/> of <see cref="InvokeAsync"/>.</param>
        /// <returns>A task that represents the execution of this middleware.</returns>
        public override async Task InvokeAsync(HttpContext context, IThrottlingCache tc)
        {
            var exception = false;

            try
            {
                await AspNetCoreInfrastructure.InvokeThrottlerSentinelAsync(context, tc, Options, async (message, response) =>
                {
                    response.StatusCode = (int)message.StatusCode;
                    response.Headers.AddOrUpdateHeaders(message.Headers);
                    await response.Body.WriteAsync(await message.Content.ReadAsByteArrayAsync());
                });
            }
            catch (ThrottlingException)
            {
                exception = true;
            }
            if (!exception)
            {
                await Next(context).ContinueWithSuppressedContext();
            }
        }