.net Core中没有继续沿用以前asp.net中的管道事件,而是开发了一个新的管道(Middleware):

    public class MiddlewareDemo
{
private readonly RequestDelegate _next; public MiddlewareDemo(RequestDelegate next)
{
_next = next;
} public Task Invoke(HttpContext httpContext)
{
//可以在此处写一些需要的代码
return _next.Invoke(httpContext);
}
}

在Startup的Configure方法中用UseMiddleware方法添加到管道中去

  app.UseMiddleware<MiddlewareDemo>();

如果将_next.Invoke(httpContext)改成Task.CompletedTask那么后续添加的Middleware都不会执行了

        public Task Invoke(HttpContext httpContext)
{
return Task.CompletedTask;
//return _next.Invoke(httpContext);
}

通过查看UseMiddleware方法源代码,发现其实际调用的是IApplicationBuilder的Use方法

模拟构建Middleware(RequestDelegate)管道

方便演示我们就创建控制台项目,代码如下:

public delegate Task RequestDelegate(Context context);
    public class Context
{ }
    class Program
{
static List<Func<RequestDelegate, RequestDelegate>> list = new List<Func<RequestDelegate, RequestDelegate>>();
static void Main(string[] args)
{
Use(next =>
{
return context =>
{
Console.WriteLine("");
return next.Invoke(context);
};
}); Use(next =>
{
return context =>
{
Console.WriteLine("");
return next.Invoke(context);
};
}); Use(next =>
{
return context =>
{
Console.WriteLine("");
return next.Invoke(context);
};
}); Build(); Console.Read();
} static void Use(Func<RequestDelegate, RequestDelegate> middleware)
{
list.Add(middleware);
} static void Build()
{
RequestDelegate endReq = (context) =>
{
Console.WriteLine("end");
return Task.CompletedTask;
};
list.Reverse(); //不反转的话最后添加的会先执行
foreach (var middleware in list)
{
endReq = middleware.Invoke(endReq);
}
       //执行到此处endReq是第一个添加到list集合中的Middleware
endReq(new Context());
}
}

运行结果:

如果在某一个Use方法中不执行next.Invoke(context),那么后续使用Use方法添加的都不会执行了,改成如下:

            Use(next =>
{
return context =>
{
Console.WriteLine("");
return Task.CompletedTask;
//return next.Invoke(context);
};
});

总结:

这样的一种模式扩展性比较好,比如一个项目中要使用MVC则写app.UseMvc来添加进去,使用Session,则用app.UseSession()。

Session/Route/Cros等都是使用Middleware来实现的

.net core 2.0学习记录(四):Middleware使用以及模拟构建Middleware(RequestDelegate)管道的更多相关文章

  1. .net core 2.0学习记录(三):内置IOC与DI的使用

    本篇的话介绍下IOC和ID的含义以及如何使用.Net Core中的DI. 一.我是这么理解IOC和DI的: IOC:没有用IOC之前是直接new实例来赋值,使用IOC之后是通过在运行的时候根据配置来实 ...

  2. .net core 2.0学习记录(一):搭建一个.Net Core网站项目

    .Net Core开发可以使用Visual Studio 2017或者Visual Studio Code,下面使用Visual Studio 2017搭建一个.net Core MVC网站项目. 一 ...

  3. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  4. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  5. JavaScript学习记录四

    title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  6. ASP.NET Core 1.0 开发记录

    官方资料: https://github.com/dotnet/core https://docs.microsoft.com/en-us/aspnet/core https://docs.micro ...

  7. 4.VUE前端框架学习记录四:Vue组件化编码2

    VUE前端框架学习记录四:Vue组件化编码2文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  8. ASP.NET 5 RC1 升级 ASP.NET Core 1.0 RC2 记录

    升级文档: Migrating from DNX to .NET Core Migrating from ASP.NET 5 RC1 to ASP.NET Core 1.0 RC2 Migrating ...

  9. 一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建

    作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把,发现速度确实是快了很多,其中也遇到一些小问题,所以整理了一些学习笔记: 阅读目录 环境说明 安装C ...

随机推荐

  1. [nginx]nginx rewrite规则之last和break

    c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...

  2. mac命令行配置网络

    mac命令行配置网络今天终于找到了Mac OS X通过命令行修改ip的方式了,记录如下: 修改mac地址,重启后失效sudo ifconfig en0 lladdr d0:67:e5:2e:07:f1 ...

  3. 元类编程-- metaclass

    #类也是对象,type创建类的类 def create_class(name): if name == "user": class User: def __str__(self): ...

  4. Spring Boot 启动报错:LoggingFailureAnalysisReporter

    17:57:19: Executing task 'bootRun'... Parallel execution with configuration on demand is an incubati ...

  5. PHP做分页查询(查询结果也显示为分页)

    1.先把数据库里所有的数据分页显示在页面,并在显示数据的表格上方加上查询表单. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  6. UIAlertView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   UIAlertView   //转载请注明出处--本文永久链接:http://ww ...

  7. {转}用ADMM求解大型机器学习问题

    [本文链接:http://www.cnblogs.com/breezedeus/p/3496819.html] 从等式约束的最小化问题说起:                               ...

  8. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

  9. 爬虫--Urllib库详解

    1.什么是Urllib? 2.相比Python2的变化 3.用法讲解 (1)urlopen urlllb.request.urlopen(url,data=None[timeout,],cahle=N ...

  10. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...