在阅读了Artech的ASP.NET Core管道深度剖析(2):创建一个“迷你版”的管道来模拟真实管道请求处理流程之后, 自己做了一个"迷你版"中的"迷你版", 对于理解中间件和HttpServer之间的关系很有帮助, 现在贴出代码如下:
/// <summary>
/// 使用控制台实现WebAppi - 简单版 - 目标: 实现一个监听 Http 的 Server, 一个连续的 Middleware
/// </summary>
public class Program
{
/// <summary>
/// 服务集合的容器: 服务类别1. 监听 Http 的 Server 服务类别2. 中间件集合
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
// 用于监听Http
var listener = new HttpListener();
listener.Prefixes.Add("http://localhost:6688/");
listener.Start();
// 用于访问HttpListener的HttpRequest和HttpResponse
HttpListenerContext c = listener.GetContext();
// 定义一个中间件
Func<Func<HttpListenerContext, Task>, Func<HttpListenerContext, Task>> middleware1 = next =>
{
return context =>
{
string content = "<html><body><h1>Hello World!</h1></body></html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//c.Response.ContentLength64 = buffer.Length; // 这里赋值的话会出的问题: 1. 一旦填充到指定的长度, 立即将response响应, 释放资源 2. 如果最终都没有到达指定的长度, 不允许释放response, 抛出异常 // 所以, 不赋值, 让他最终自动生成就行了
context.Response.ContentType = "text/html";
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
return next(context);
};
};
// 定义另一个中间件
Func<Func<HttpListenerContext, Task>, Func<HttpListenerContext, Task>> middleware2 = next =>
{
return context =>
{
string content = "<script>alert('Hello World')</script>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
c.Response.OutputStream.Write(buffer, 0, buffer.Length);
return next(context);
};
};
var task = middleware1(middleware2(cc => Task.Delay(1)))(c);
task.Wait();
c.Response.Close(); // 将响应发送給客户端, 释放response的资源
listener.Stop(); // 使实例停止接收客户端请求
}
/// 使用控制台实现WebAppi - 简单版 - 目标: 实现一个监听 Http 的 Server, 一个连续的 Middleware
/// </summary>
public class Program
{
/// <summary>
/// 服务集合的容器: 服务类别1. 监听 Http 的 Server 服务类别2. 中间件集合
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
// 用于监听Http
var listener = new HttpListener();
listener.Prefixes.Add("http://localhost:6688/");
listener.Start();
// 用于访问HttpListener的HttpRequest和HttpResponse
HttpListenerContext c = listener.GetContext();
// 定义一个中间件
Func<Func<HttpListenerContext, Task>, Func<HttpListenerContext, Task>> middleware1 = next =>
{
return context =>
{
string content = "<html><body><h1>Hello World!</h1></body></html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//c.Response.ContentLength64 = buffer.Length; // 这里赋值的话会出的问题: 1. 一旦填充到指定的长度, 立即将response响应, 释放资源 2. 如果最终都没有到达指定的长度, 不允许释放response, 抛出异常 // 所以, 不赋值, 让他最终自动生成就行了
context.Response.ContentType = "text/html";
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
return next(context);
};
};
// 定义另一个中间件
Func<Func<HttpListenerContext, Task>, Func<HttpListenerContext, Task>> middleware2 = next =>
{
return context =>
{
string content = "<script>alert('Hello World')</script>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
c.Response.OutputStream.Write(buffer, 0, buffer.Length);
return next(context);
};
};
var task = middleware1(middleware2(cc => Task.Delay(1)))(c);
task.Wait();
c.Response.Close(); // 将响应发送給客户端, 释放response的资源
listener.Stop(); // 使实例停止接收客户端请求
}
}
![](https://images2015.cnblogs.com/blog/737425/201606/737425-20160612162101668-586319842.jpg)
运行以后直接在浏览器输入localhost:6688即可, GitHub地址为: https://github.com/HeabKing/DotNetCoreStudy