一、Program.cs类与Startup类

1、一切从Main开始,Main方法包含了是整个应用程序的入口

ASP.NET Core应用程序可以配置和启动主机(Host)。

主机负责应用程序启动和生存期管理,配置服务器和请求处理管道。

主机还可以设置日志记录、依赖关系注入和配置。

而host主机又包括Web主机(IWebHostBuilder)和通用主机(IHostBuilder)。

根据官网:

1、可选择性地包括 ConfigureServices 方法以配置应用的服务。

2、必须包括 Configure 方法以创建应用的请求处理管道。

Startup 方法体如下:

public class Startup
{
// 使用此方法向容器添加服务
public void ConfigureServices(IServiceCollection services)
{
...
}
// 使用此方法配置HTTP请求管道
public void Configure(IApplicationBuilder app)
{
...
}
}

二、进程托管(进程内托管、进程外托管)

1、进程内托管

1、进程外托管

进程内托管还是进程外托管区别:

可以通过获取进程名:

三、Web服务器

1、有2个Web服务器-内部Web服务器和外部Web服务器:

内部服务器是Kestrel,外部服务器可以是IIS、Nginx、Apache

2、什么是Kestrel Web Server?

Kestrel是ASP.NET Core的跨平台Web服务器。

Kestrel中用于托管应用程序的进程是dotnet.exe

四、中间件

什么是中间件?

ASP.NET Core 中 HTTP 管道使用中间件组合处理的方式,是能够处理Http请求或者响应请求。

而中间件(Middleware)是ASP.NET Core中的一个重要特性。

所谓中间件就是嵌入到应用管道中用于处理请求和响应的一段代码。

对于写代码的人而言,一切皆中间件。

业务逻辑/数据访问/等等一切都需要以中间件的方式来呈现。

ASP.NET Core Middleware可以分为两种类型:

1、Conventional Middleware

2、IMiddleware

如何实现自定义中间件:

一、内联中间件(in-line middleware)

public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
// Do work that doesn't write to the Response.
await next.Invoke();
// Do logging or other work that doesn't write to the Response.
});
}
}

IApplicationBuilder 定义用于配置应用请求管道的类

IApplicationBuilder的扩展方法:RunMapMapWhen
Use(this IApplicationBuilder app, Func<HttpContext, Func<Task>, Task> middleware)

最终都会调用IApplicationBuilder接口中的

Use(Func<RequestDelegate, RequestDelegate> middleware)

方法来实现向请求处理管道中注入中间件,后面会对源码做分析。

演示:

1、通常新建一个空的 ASP.NET Core Web Application

在启动类里可以看到这么一句:

// Startup.cs
// ...
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
// ...

这就是一个匿名函数实现的中间件,虽然内容比较少。

可以看到通过匿名函数实现的中间件是内嵌在启动类文件中的,因此通常也叫做内联中间件。

具体案例:

修改启动类代码如下:

// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System; namespace CoreTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} // 使用匿名函数实现一个内联中间件
app.Use(async (context, next) =>
{
throw new NotImplementedException("一个使用匿名函数,但未实现具体内容的内联中间件");
}); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}

这里我们在 app.Run 之前使用 app.Use 添加一个匿名函数实现的内联中间件,按照中间件的注册顺序,

当发起请求时,会抛出一个异常 NotImplementedException("一个使用匿名函数,但未实现具体内容的内联中间件")。

 F5 启动下,看看页面:

再来调整下启动类,代码如下:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; namespace CoreTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} // 使用匿名函数实现一个内联中间件
app.Use(async (context, next) =>
{
// 这里不对 request 做任何处理,直接调用下一个中间件
await next.Invoke();
}); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}

这里我们在 app.Run 之前使用 app.Use 添加一个匿名函数实现的内联中间件,该中间件没有对 request 做任何处理,

只是一个空的空间件,按照中间件的注册顺序,当发起请求时,页面应该显示 Hello World!.

匿名函数不是很直观,但是用内联的方式可以快速开始一些开发,不用新建一个中间件类,不用专门想个不一样的名字,小场景下是非常方便实用的。

2、实现接口(自定义中间件类)

通过实现接口 IMiddleware 编写自定义中间件,这是一种强类型的方式,我们需要必须强制按照接口的定义来实现.

接口 IMiddleware 定义如下:

using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Http
{
public interface IMiddleware
{
Task InvokeAsync(HttpContext context, RequestDelegate next);
}
}

可以看到接口 IMiddleware 的命名空间是 Microsoft.AspNetCore.Http,需要实现的方法是InvokeAsync()

// 新建类 MyMiddleware.cs
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace CoreTest
{
public class MyMiddleware : IMiddleware
{
public Task InvokeAsync(HttpContext context, RequestDelegate next)
{
throw new NotImplementedException();
}
}
}

按照上面实现的中间件 MyMiddleware,在执行时应该会抛出 NotImplementedException.

使用接口实现的中间件需要在先在服务容器中注册否则

具体代码注册代码:

// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; namespace WebApplication1
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// 在服务容器中注册自定义中间件
services.AddSingleton<MyMiddleware>();
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} // 使用 UseMiddleware() 把自定义中间件添加到管道中
app.UseMiddleware<MyMiddleware>(); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}

然后 F5 启动,页面上可以看到如下结果:

抛出了一个 NotImplementedException.

改造下 MyMiddleware 中间件

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace CoreTest
{
public class MyMiddleware: IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
// 这里不对 request 做任何处理,直接调用下一个中间件
await next(context);
}
}
}

这里相当于我们实现了一个叫做 MyMiddleware 的中间件,但是并没有对请求进行任何处理,页面上应该正常显示 Hello World! 字符串.

F5 启动看看

3、约定方式(自定义中间件类)

编程世界有这么一句话,叫"约定大于配置".

那么编写中间件的约定是什么呢?

新建一个类,类名叫做 MyMiddleware 好了,代码如下:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace CoreTest
{
public class MyMiddleware
{
// 1. 需要实现一个构造函数,参数为 RequestDelegate
public MyMiddleware(RequestDelegate next)
{ }
// 2. 需要实现一个叫做 InvokeAsync 方法
public async Task InvokeAsync(HttpContext context)
{
throw new NotImplementedException("这是一个按照约定方式编写的中间件,但未实现具体内容");
}
}
}

约定的内容,就是满足2个需要...不满足需要则异常.

然后我们把这个中间件,注册到管道中,以便使用

// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; namespace CoreTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} // 注册自定义中间件
// 注册顺序=1
app.UseMiddleware<MyMiddleware>(); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}

F5 启动,来看看效果

调整下中间件,让请求能正常响应输出 Hello World!

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace CoreTest
{
public class MyMiddleware
{
private readonly RequestDelegate _next;
// 需要实现一个构造函数,参数为 RequestDelegate
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
// 需要实现一个叫做 InvokeAsync 方法
public async Task InvokeAsync(HttpContext context)
{
// 不处理任何 request, 直接调用下一个中间件
await _next.Invoke(context);
}
}
}

F5 启动,看看效果

五、管道

ASP.NET Core的处理流程是一个管道,而中间件是装配到管道中的用于处理请求和响应的组件

最新文章

  1. iOS 模拟器变的很多的问题
  2. 整理分享C#通过user32.dll模拟物理按键操作的代码
  3. Java基础复习笔记系列 八 多线程编程
  4. Sencha Touch 2.2 Store Proxy 异常监控
  5. 做技术最自由,在IT最幸福!
  6. js从身份证号中获取出生日期和性别
  7. mysql 5.6 binlog组提交1
  8. el表达式跟ognl表达式的区别(转)
  9. oracle 不转义 &amp;
  10. 使用OC开发phonegp 组件
  11. Android Navigation Drawer(导航抽屉)
  12. 解决Linux下面Firefox无法播放mp3的问题
  13. vue.js初探:计算属性和methods
  14. HDU--1164
  15. Spring boot之hello word
  16. [特别公告]RDIFramework.NET微信公众号迁移通知
  17. 【原创】大叔经验分享(9)yarn重要配置yarn.nodemanager.local-dirs
  18. IO流(三)
  19. linux proc
  20. 十九、Linux 进程与信号---环境表

热门文章

  1. RabbitMQ(Exchange交换机详解)(四)
  2. fedora18 Cannot retrieve metalink for repository: fedora. Please verify its path and try again 解决方法
  3. cdn for js library
  4. win7x64 串口程序无法运行,提示:component &#39;MSCOMM32.OCX&#39; or one of its dependencies not correctlu registered。。。
  5. Linux中的NetworkManager网络管理
  6. 图论&amp;线性基(?)(8.12)
  7. Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
  8. 部署-GPS授时系统:GPS授时系统
  9. chales抓包,模拟异常情况
  10. FutureTask的用法以及两种常用的使用场景