Beispiel #1
0
        public static void Display()
        {
            var _applicationBuilder = new ApplicationBuilder();

            Func <ResquestDelegate, ResquestDelegate> middleware = x =>
            {
                //当前要执行的委托
                ResquestDelegate _currentDelegate = async context =>
                {
                    // await  context.ResponseAsync("执行的第一个中间件开始");
                    Console.WriteLine("执行的第一个中间件开始");
                    await x(context);

                    Console.WriteLine("执行的第一个中间件结束");
                };
                return(_currentDelegate);
            };
            Func <ResquestDelegate, ResquestDelegate> middleware2 = x =>
            {
                //当前要执行的委托
                ResquestDelegate _currentDelegate = async context =>
                {
                    //await context.ResponseAsync("执行的第二个中间件开始");
                    Console.WriteLine("执行的第二个中间件开始");
                    await x(context);

                    Console.WriteLine("执行的第二个中间件结束");
                };
                return(_currentDelegate);
            };

            try
            {
                _applicationBuilder.Use(middleware);
                _applicationBuilder.Use(middleware2);
                _applicationBuilder.Run(x =>
                {
                    Console.WriteLine("结束中间件");
                    return(Task.CompletedTask);
                });
                var lastDelegate = _applicationBuilder.Build();

                lastDelegate(new HttpContext());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();
            }
        }
Beispiel #2
0
 public ApplicationBuilder Run(ResquestDelegate EndMethod)
 {
     _endMethod = EndMethod;
     return(this);
 }