/// <summary> /// Initializes a new instance. /// </summary> public TimerHttpMiddleware(FluentHttpRequestDelegate next, TimerHttpMiddlewareOptions options, ILogger <TimerHttpMiddleware> logger) { _next = next; _options = options; _logger = logger; if (_options.WarnThreshold <= TimeSpan.Zero) { throw new ArgumentException($"{nameof(_options.WarnThreshold)} must be greater than Zero."); } }
/// <summary> /// Initializes a new instance. /// </summary> public LoggerHttpMiddleware(FluentHttpRequestDelegate next, LoggerHttpMiddlewareOptions options, ILogger <LoggerHttpMiddleware> logger) { _next = next; _options = options; _logger = logger; }
/// <summary> /// Run specified middleware, and finally send the given request. /// </summary> /// <param name="middlewareCollection">Middleware to pipe.</param> /// <param name="request">Request to send.</param> /// <param name="send">Actual send function.</param> /// <returns>Returns response.</returns> public async Task <FluentHttpResponse> Run(IList <MiddlewareConfig> middlewareCollection, FluentHttpRequest request, FluentHttpRequestDelegate send) { if (middlewareCollection.Count == 0) { return(await send(request)); } FluentHttpResponse httpResult = null; IFluentHttpMiddleware previousMiddleware = null; for (int i = middlewareCollection.Count; i-- > 0;) { request.CancellationToken.ThrowIfCancellationRequested(); var middlewareOptions = middlewareCollection[i]; var isLast = middlewareCollection.Count - 1 == i; var isFirst = i == 0; var next = isLast ? send : previousMiddleware.Invoke; object[] ctor; if (middlewareOptions.Args == null) { ctor = new object[] { next } } ; else { ctor = new object[middlewareOptions.Args.Length + 1]; ctor[0] = next; Array.Copy(middlewareOptions.Args, 0, ctor, 1, middlewareOptions.Args.Length); } var instance = (IFluentHttpMiddleware)ActivatorUtilities.CreateInstance(_serviceProvider, middlewareOptions.Type, ctor); if (isFirst) { httpResult = await instance.Invoke(request); } else { previousMiddleware = instance; } } return(httpResult); }
public TestHttpMiddleware(FluentHttpRequestDelegate next) { _next = next; }