コード例 #1
0
        public async void Use()
        {
            var    provider = new ServiceCollection().AddScoped <IService, Service>().BuildServiceProvider();
            var    builder  = new InterceptorChainBuilder(provider);
            string value    = null;

            _intercept = () => value = Guid.NewGuid().ToString();
            var interceptorPipeline = builder.Use <FoobarInterceptor>(1, "abc").Build();

            await interceptorPipeline(context => Task.CompletedTask)(new FoobarInvocationContext());

            Assert.NotNull(value);
        }
コード例 #2
0
        public async void Use_And_Build()
        {
            var    builder = new InterceptorChainBuilder(new ServiceCollection().BuildServiceProvider());
            string value   = "";

            InterceptorDelegate interceptor1 = next => (async context => { value += "1"; await next(context); });
            InterceptorDelegate interceptor2 = next => (async context => { value += "2"; await next(context); });
            InterceptorDelegate interceptor3 = next => (context => { value += "3"; return(Task.CompletedTask); });
            InterceptorDelegate interceptor4 = next => (async context => { value += "4"; await next(context); });

            builder
            .Use(interceptor2, 2)
            .Use(interceptor4, 4)
            .Use(interceptor1, 1)
            .Use(interceptor3, 3);

            var chain = builder.Build();

            await chain(context => Task.CompletedTask)(new FoobarInvocationContext());

            Assert.Equal("123", value);
        }
コード例 #3
0
        public void Use_Arguments_Not_Allow_Null()
        {
            var builder = new InterceptorChainBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.Throws <ArgumentNullException>(() => builder.Use(null, 1));
        }