簡體   English   中英

C#異步和等待流

[英]C# Async and Await flow

我試圖了解異步並等待流程。 開始查看代碼
John Atten關於Owin和Katana 的博客 在嘗試查看執行步驟時,我在流程中找到了幾個步驟(步驟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

流:

進入 - >第一個中間件

  1. IOwinContext context = new OwinContext(environment);
  2. await context.Response.WriteAsync("<h1> Middleware:1 -> Hello from X1</h1>"); // Response Sent to browser
  3. await next.Invoke(environment);

進入 - >第二個中間件

  1. IOwinContext context = new OwinContext(environment);

  2. await context.Response.WriteAsync("<h1> Middleware:2 -> Hello from X2</h1>");// Response Sent to browser

  3. await next.Invoke(environment);

進入(回到呼叫方法) - >第一個中間件

  1. await next.Invoke(environment);

  2. 退出 - > FirstMiddleWare

重新進入 - >第一個中間件

  1. IOwinContext context = new OwinContext(environment);

  2. await context.Response.WriteAsync("<h1> Middleware:1 -> Hello from X1</h1>"); // No Response Sent to browser

  3. await next.Invoke(environment);

重新進入 - >第二個中間件

  1. IOwinContext context = new OwinContext(environment);

  2. await context.Response.WriteAsync("<h1> Middleware:2 -> Hello from X2</h1>");// No Response Sent to browser

  3. await next.Invoke(environment);

重新進入(返回呼叫方法) - >第一個中間件

  1. await next.Invoke(environment);

  2. 退出 - > FirstMiddleWare

執行停止我的問題是為什么它再次遍歷步驟9和16?

並添加到它,即使遍歷步驟9和16,響應也不會改變。 所以我猜它在那里進行一些后置條件檢查。

**編輯 - 1 **

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

遍歷步驟9到16是因為您從瀏覽器獲取了favicon的自動第二個請求。 如果您注意context.Request.Path,您會注意到第一輪中間件是“/”,第二輪是“/favicon.ico”。 實際上,行為取決於您用於發出請求的瀏覽器。 我已經使用最新版本的幾個瀏覽器和Webkit瀏覽器(Chrome和Opera)以及IE測試我總是得到一個關於favicon的請求。 使用FF我只是第一次得到它並且它被緩存。 而使用Edge - 根本沒有要求它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM