Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#异步和等待流_C#_Async Await - Fatal编程技术网

C#异步和等待流

C#异步和等待流,c#,async-await,C#,Async Await,我试图理解异步并等待流。从 关于奥文和卡塔纳。在尝试查看执行步骤时,我在流程中发现了几个步骤(步骤9和16),我无法理解为什么执行会遍历这些步骤 代码: 使用系统; 使用System.Collections.Generic; 使用System.Threading.Tasks; 使用Microsoft.Owin; 使用Microsoft.Owin.Hosting; 使用Owin; 命名空间KatanaConsole { //为OWIN APPFunc使用别名 使用AppFunc=Func; 班级计

我试图理解异步并等待流。从
关于奥文和卡塔纳。在尝试查看执行步骤时,我在流程中发现了几个步骤(步骤9和16),我无法理解为什么执行会遍历这些步骤

代码:

使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用Microsoft.Owin;
使用Microsoft.Owin.Hosting;
使用Owin;
命名空间KatanaConsole
{
//为OWIN APPFunc使用别名
使用AppFunc=Func;
班级计划
{
静态void Main(字符串[]参数)
{
var uri=”http://localhost:8080";
使用(WebApp.Start(uri))
{
WriteLine(“Web服务器在端口2323上启动”);
WriteLine(“服务器已启动;按enter键退出”);
Console.ReadLine();
}
}
}
公营创业
{
公共无效配置(IAppBuilder appBuilder)
{
var firstMiddleware=newfunc(firstMiddleware);
var secondMiddleware=newfunc(secondMiddleware);
使用(第一中间件);
appBuilder.Use(第二中间件);
}
公共AppFunc第一中间件(AppFunc下一个)
{
AppFunc AppFunc=异步环境=>
{
IOwinContext上下文=新的OwinContext(环境);
wait context.Response.WriteAsync(“中间件:1->Hello from X1”);
等待下一步。调用(环境);
};
返回appFunc;
}
公共AppFunc第二中间件(AppFunc下一个)
{
AppFunc AppFunc=异步(IDictionary环境)=>
{
IOwinContext上下文=新的OwinContext(环境);
wait context.Response.WriteAsync(“中间件:2->Hello from X2”);
等待下一步。调用(环境);
};
返回appFunc;
}
}
}
尝试访问localhost:8080时的代码流

流量:

进入->第一个中间件

  • IOwinContext上下文=新的OwinContext(环境)
  • wait context.Response.WriteAsync(“中间件:1->Hello from
    X1“;//响应发送到浏览器
  • wait next.Invoke(环境)
  • 进入->第二个中间件

  • IOwinContext上下文=新的OwinContext(环境)

  • wait context.Response.WriteAsync(“中间件:2->Hello from
    X2“;//响应发送到浏览器

  • wait next.Invoke(环境)

  • 进入(返回调用方法)->第一个中间件

  • wait next.Invoke(环境)

  • 退出->第一中间件

  • 重新进入->第一个中间件

  • IOwinContext上下文=新的OwinContext(环境)

  • wait context.Response.WriteAsync(“中间件:1->Hello from
    X1“;//未向浏览器发送响应

  • wait next.Invoke(环境)

  • 重新进入->第二个中间件

  • IOwinContext上下文=新的OwinContext(环境)

  • wait context.Response.WriteAsync(“中间件:2->Hello from
    X2“;//未向浏览器发送响应

  • wait next.Invoke(环境)

  • 重新输入(返回调用方法)->第一个中间件

  • wait next.Invoke(环境)

  • 退出->第一中间件

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

    再加上,即使遍历了步骤9和16,响应也不会改变。所以我猜它是在做一些状态后检查

    **编辑-1**


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

    由于浏览器自动发出第二次请求,因此已遍历步骤9至16。如果您注意context.Request.Path,您会注意到中间件的第一轮是“/”而第二轮是“/favicon.ico”。实际上,行为取决于您用于发出请求的浏览器。我已经用最新版本的浏览器和Webkit浏览器(Chrome和Opera)加IE进行了测试,我总是收到favicon的请求。有了FF,我第一次就得到了它,它正在被缓存。还有Edge——根本没有人要求它

    请缩小你的问题范围,使之与标题更一致。它太宽泛了,看起来更像是一个代码审查请求。谢谢和问候,这个问题对我来说似乎没问题,而且确实很奇怪!似乎第二个中间件的“next”应该为null或与之等效的值。您是否真的在页面中看到中间件:1 Hello两次?@TheMar我在输出中只看到中间件:1次。
    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;
            }
        }
    
    
    }