C # Async and Await flow

I am trying to understand async and wait for the stream. He began to look at the code from
John Atten 's blog about Aubin and Katan. Looking through the execution steps, I found several steps in the thread (Steps 9 and 16) that I cannot understand about how the execution will be executed.

the code:

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;
        }
    }


}

Code flow when trying to access localhost: 8080

Flow:

Enter → First Middleware

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

Enter -> Second Middleware

  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);

Enter (Return to Call Method) → First Middleware

  1. await next.Invoke(environment);

  2. Exit -> FirstMiddleWare

Re-Enters → First Middleware

  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);

Re-enable → Second Middle Software

  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);

Re-enable (Back to the calling method) → First middleware

  1. await next.Invoke(environment);

  2. Exit -> FirstMiddleWare

, 9 16?

, 9 16, . , .

** - 1 **

, ( 9 16)

+4
1

9 16 - , favicon. context.Request.Path, , "/", "/favicon.ico". , , . Webkit (Chrome Opera), IE, . FF , . Edge - .

+1

All Articles