コード例 #1
0
        public async Task Authorize()
        {
            Assert.False(_httpContext.User.HasClaim("role", "Admin"));

            _configuration[GraphQLService.SecretTokenKey] = _secretToken;
            _httpContext.Request.Headers.Add("Authorization", $"Basic {_secretToken}");

            await _middleware.InvokeAsync(
                _httpContext,
                _requestDelegate);

            Assert.True(_httpContext.User.HasClaim("role", "Admin"));
        }
コード例 #2
0
        public static void UseMiddleware(
            this SimpleInjectorUseOptions options, Type middlewareType, IApplicationBuilder app)
        {
            Requires.IsNotNull(options, nameof(options));
            Requires.IsNotNull(middlewareType, nameof(middlewareType));
            Requires.IsNotNull(app, nameof(app));

            Requires.ServiceIsAssignableFromImplementation(
                typeof(IMiddleware), middlewareType, nameof(middlewareType));
            Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType));

            var container = options.Container;

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType);

            // By creating an InstanceProducer up front, it will be known to the container, and will be part
            // of the verification process of the container.
            InstanceProducer <IMiddleware> producer =
                lifestyle.CreateProducer <IMiddleware>(middlewareType, container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });
        }
コード例 #3
0
        private static IPipelineBuilder <TService, TContext> UseMiddlewareInterface <TService, TContext>(IPipelineBuilder <TService, TContext> app, Type middlewareType)
            where TService : BaseDomainService
            where TContext : IRequestContext
        {
            return(app.Use(next =>
            {
                return async context =>
                {
                    IMiddlewareFactory <TContext> middlewareFactory = (IMiddlewareFactory <TContext>)context.RequestServices.GetService(typeof(IMiddlewareFactory <TContext>));
                    if (middlewareFactory == null)
                    {
                        // No middleware factory
                        throw new InvalidOperationException("UseMiddlewareNoMiddlewareFactory(typeof(IMiddlewareFactory))");
                    }

                    IMiddleware <TContext> middleware = middlewareFactory.Create(middlewareType);
                    if (middleware == null)
                    {
                        // The factory returned null, it's a broken implementation
                        throw new InvalidOperationException("UseMiddlewareUnableToCreateMiddleware(middlewareFactory.GetType(), middlewareType)");
                    }

                    try
                    {
                        await middleware.InvokeAsync(context, next);
                    }
                    finally
                    {
                        middlewareFactory.Release(middleware);
                    }
                };
            }));
        }
コード例 #4
0
        /// <summary>
        /// Adds a middleware type to the application's request pipeline. The middleware will be resolved from
        /// the supplied the Simple Injector <paramref name="container"/>. The middleware will be added to the
        /// container for verification.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
        /// <param name="middlewareType">The middleware type that needs to be applied. This type must
        /// implement <see cref="IMiddleware"/>.</param>
        /// <param name="container">The container to resolve <paramref name="middlewareType"/> from.</param>
        /// <returns>The supplied <see cref="IApplicationBuilder"/> instance.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null reference.</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="middlewareType"/> does not
        /// derive from <see cref="IMiddleware"/>, is an open-generic type, or not a concrete constructable
        /// type.</exception>
        public static IApplicationBuilder UseMiddleware(
            this IApplicationBuilder app, Type middlewareType, Container container)
        {
            Requires.IsNotNull(app, nameof(app));
            Requires.IsNotNull(middlewareType, nameof(middlewareType));
            Requires.IsNotNull(container, nameof(container));

            Requires.ServiceIsAssignableFromImplementation(
                typeof(IMiddleware), middlewareType, nameof(middlewareType));

            Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType));

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType);

            // By creating an InstanceProducer up front, it will be known to the container, and will be part
            // of the verification process of the container.
            // Note that the middleware can't be registered in the container, because at this point the
            // container might already be locked (which will happen when the new ASP.NET Core 3 Host class is
            // used).
            InstanceProducer <IMiddleware> producer =
                lifestyle.CreateProducer <IMiddleware>(middlewareType, container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });

            return(app);
        }
        /// <summary>
        /// Adds a middleware type to the application's request pipeline. The middleware will be resolved from the supplied
        /// the Simple Injector <paramref name="container"/>. The middleware will be added to the container for verification.
        /// </summary>
        /// <typeparam name="TMiddleware">The middleware type.</typeparam>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
        /// <param name="container">The container to resolve <typeparamref name="TMiddleware"/> from.</param>
        /// <returns>The supplied <see cref="IApplicationBuilder"/> instance.</returns>
        public static IApplicationBuilder UseMiddleware <TMiddleware>(this IApplicationBuilder app, Container container)
            where TMiddleware : class, IMiddleware
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(typeof(TMiddleware));

            // By creating an InstanceProducer up front, it will be known to the container, and will be part of the
            // verification process of the container.
            InstanceProducer <IMiddleware> producer = lifestyle.CreateProducer <IMiddleware, TMiddleware>(container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });

            return(app);
        }
コード例 #6
0
ファイル: AppBuilder.cs プロジェクト: vincentshow/CefBox
        public AppBuilder Use <TMiddleWare>(Func <TMiddleWare> getInstance) where TMiddleWare : IMiddleware
        {
            return(this.Use(next =>
            {
                IMiddleware middleware = getInstance.Invoke();

                if (middleware == null)
                {
                    throw new InvalidOperationException($"cannot get instance of type {typeof(TMiddleWare).ToString()}");
                }

                return async context =>
                {
                    await middleware.InvokeAsync(context, next);
                };
            }));
        }
コード例 #7
0
        /// <summary>
        /// Adds a middleware type to the application's request pipeline. The middleware will be resolved
        /// from Simple Injector. The middleware will be added to the container for verification.
        /// </summary>
        /// <typeparam name="TMiddleware">The middleware type.</typeparam>
        /// <param name="options">The <see cref="SimpleInjectorUseOptions"/>.</param>
        /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
        public static void UseMiddleware <TMiddleware>(
            this SimpleInjectorUseOptions options, IApplicationBuilder app)
            where TMiddleware : class, IMiddleware
        {
            Requires.IsNotNull(options, nameof(options));
            Requires.IsNotNull(app, nameof(app));

            var container = options.Container;

            var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(typeof(TMiddleware));

            // By creating an InstanceProducer up front, it will be known to the container, and will be part
            // of the verification process of the container.
            InstanceProducer <IMiddleware> producer =
                lifestyle.CreateProducer <IMiddleware, TMiddleware>(container);

            app.Use((c, next) =>
            {
                IMiddleware middleware = producer.GetInstance();
                return(middleware.InvokeAsync(c, _ => next()));
            });
        }
コード例 #8
0
        public void InvokeAsync_SetsActivityPropetry()
        {
            HttpContext context1 = HttpContextHelper.GetContextWithIp("192.168.0.4");
            HttpContext context2 = HttpContextHelper.GetContextWithIp("127.0.0.4");

            IMiddleware middleware = GetMiddelware();

            string GetActivityUserHash(HttpContext context)
            {
                Activity activity = new Activity(nameof(InvokeAsync_SetsActivityPropetry)).Start();

                middleware.InvokeAsync(context, c => Task.CompletedTask);
                activity.Stop();
                return(activity.GetUserHash());
            }

            string hash1 = GetActivityUserHash(context1);
            string hash2 = GetActivityUserHash(context2);

            Assert.AreNotEqual(hash1, hash2);
            Assert.AreEqual(hash1, GetActivityUserHash(context1));
            Assert.AreEqual(hash2, GetActivityUserHash(context2));
        }
コード例 #9
0
 private static IApplicationBuilder UseMiddlewareInterface(IApplicationBuilder app, Type middlewareType)
 {
     return(app.Use((RequestDelegate next) => async delegate(HttpContext context)
     {
         IMiddlewareFactory middlewareFactory = (IMiddlewareFactory)context.RequestServices.GetService(typeof(IMiddlewareFactory));
         if (middlewareFactory == null)
         {
             throw new InvalidOperationException(FormatException(typeof(IMiddlewareFactory)));
         }
         IMiddleware middleware = middlewareFactory.Create(middlewareType);
         if (middleware == null)
         {
             throw new InvalidOperationException(FormatException(middlewareFactory.GetType(), middlewareType));
         }
         try
         {
             await middleware.InvokeAsync(context, next);
         }
         finally
         {
             middlewareFactory.Release(middleware);
         }
     }));
 }
コード例 #10
0
 private static void UseMiddleware(IApplicationBuilder app, IMiddleware middleware) =>
 app.Use((c, next) => middleware.InvokeAsync(c, _ => next()));
コード例 #11
0
 public async Task HandlerB0T0P0E0D0()
 {
     await _middleware1.InvokeAsync(CreateHttpContext("b0t0p0e0d0"), FinishInvokeChain).ConfigureAwait(false);
 }
コード例 #12
0
 /// <inheritdoc />
 public Task InvokeAsync(HttpContext context, RequestDelegate next)
 {
     return(_etag.InvokeAsync(context, next));
 }