C#异步和等待流

我试图了解异步并等待流程.开始查看代码

关于Owin和Katana的
John Atten’s blog.在尝试查看执行步骤时,我在流程中找到了几个步骤(步骤9和16),我无法理解为什么执行将遍历.

码:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
namespace KatanaConsole
{
    //use an alias for OWIN APPFunc
    using AppFunc= Func<IDictionary<string,object>,Task>;
    class Program
    {
        static void Main(string[] args)
        {

            var uri = "http://localhost:8080";

            using ( WebApp.Start<StartUp>(uri))
            {
                Console.WriteLine("Web Server started on port 2323");
                 Console.WriteLine("Server Started; Press enter to Quit");
                Console.ReadLine();
            }
        }
    }

    public class StartUp
    {
        public void Configuration(IAppBuilder appBuilder)
        {
        var firstMiddleware= new Func<AppFunc,AppFunc>(FirstMiddleware);
        var secondMiddleware = new Func<AppFunc, AppFunc>(SecondMiddleware);

        appBuilder.Use(firstMiddleware);
        appBuilder.Use(secondMiddleware);

        }


        public AppFunc FirstMiddleware(AppFunc next)
        {

            AppFunc appFunc = async environment =>
            {
                IOwinContext context = new OwinContext(environment);
                await context.Response.WriteAsync("<h1> Middleware:1 ->  Hello from X1</h1>");
                await next.Invoke(environment);
            };
            return appFunc;
        }

        public AppFunc SecondMiddleware(AppFunc next)
        {

            AppFunc appFunc = async (IDictionary<string, object> environment) =>
            {
                IOwinContext context = new OwinContext(environment);
                await context.Response.WriteAsync("<h1> Middleware:2 ->  Hello from X2</h1>");
                await next.Invoke(environment);
            };
            return appFunc;
        }
    }


}

我尝试访问localhost时的代码流:8080

流:

进入 – >第一个中间件

> IOwinContext context = new OwinContext(environment);
> await context.Response.WriteAsync(“< h1> Middleware:1 – > Hello from from
X1< / h1>“); //响应发送到浏览器
>等待next.Invoke(环境);

进入 – >第二个中间件

> IOwinContext context = new OwinContext(environment);
> await context.Response.WriteAsync(“< h1> Middleware:2 – > Hello from from
X2< / h1>“); //响应发送到浏览器
>等待next.Invoke(环境);

进入(返回呼叫方法) – >第一个中间件

>等待next.Invoke(环境);
>退出 – > FirstMiddleWare

重新进入 – >第一个中间件

> IOwinContext context = new OwinContext(environment);
> await context.Response.WriteAsync(“< h1> Middleware:1 – > Hello from from
 X1< / h1>“); //无响应发送到浏览器
>等待next.Invoke(环境);

重新进入 – >第二个中间件

> IOwinContext context = new OwinContext(environment);
> await context.Response.WriteAsync(“< h1> Middleware:2 – > Hello from from
      X2< / h1>“); //无响应发送到浏览器
>等待next.Invoke(环境);

重新进入(返回呼叫方法) – >第一个中间件

>等待next.Invoke(环境);
>退出 – > FirstMiddleWare

执行停止
我的问题是为什么它再次遍历第9步和第16步?

并添加到它,即使遍历步骤9和16,响应也不会改变.所以我猜它在那里进行一些后置条件检查.

**编辑 – 1 **

添加了所有遍历的步骤以使其更加清晰(添加了步骤9到16)

最佳答案 遍历步骤9到16是因为您从浏览器获取
favicon的自动第二个请求.如果您注意context.Request.Path,您会注意到第一轮中间件是“/”而第二轮是为“/favicon.ico”.实际上,行为取决于您用于发出请求的浏览器.我已经使用最新版本的几个浏览器和Webkit浏览器(Chrome和Opera)以及IE测试我总是得到一个关于favicon的请求.使用FF我只是第一次得到它并且它被缓存.而使用Edge – 根本没有要求它.

点赞