AspNet WebAPI后端框架消息处理机制(配置属性HttpConfiguration.MessageHandlers)


消息处理程序 (Message Handler)
消息处理程序(Message Handler)接收HTTP请求并返回一个HTTP响应的类。Message Handler继承 HttpMessageHandler 类。HttpConfiguration.MessageHandlers是个消息集合属性,用户可以添加自定义消息。
通常是一系列消息处理程序协同工作,当第一个Message Handler接收HTTP请求,进行一些处理,并将请求提供给下一个Message Handler,系统逐个处理HttpConfiguration.MessageHandlers消息集合的对象。
消息处理程序还可以用于系统安全处理和控制,当客户端访问WebApi接口,第一时间判断请求的合法性和安全性,比如判断IP地址是否黑名单、是否恶意访问接口等。
HttpConfiguration.MessageHandlers属性:
C# Code:
namespace System.Web.Http
{
//
// 摘要:
// Represents a configuration of System.Web.Http.HttpServer instances.
public class HttpConfiguration : IDisposable
{
//....省略代码....
//
// 摘要:
// Gets an ordered list of System.Net.Http.DelegatingHandler instances to be invoked
// as an System.Net.Http.HttpRequestMessage travels up the stack and an System.Net.Http.HttpResponseMessage
// travels down in stack in return.
//
// 返回结果:
// The message handler collection.
public Collection<DelegatingHandler> MessageHandlers { get; }
//....省略代码....
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
{
//
// 摘要:
// Represents a configuration of System.Web.Http.HttpServer instances.
public class HttpConfiguration : IDisposable
{
//....省略代码....
//
// 摘要:
// Gets an ordered list of System.Net.Http.DelegatingHandler instances to be invoked
// as an System.Net.Http.HttpRequestMessage travels up the stack and an System.Net.Http.HttpResponseMessage
// travels down in stack in return.
//
// 返回结果:
// The message handler collection.
public Collection<DelegatingHandler> MessageHandlers { get; }
//....省略代码....
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
WebApi服务器端消息处理管道
在WebApi服务器端,Web API管道使用一些内置的消息处理程序:
Text Code:
1.HttpServer 从主机获取请求。
2.HttpRoutingDispatcher 根据请求进行路由。
3.HttpControllerDispatcher 将请求发送到Web API控制器。
2.HttpRoutingDispatcher 根据请求进行路由。
3.HttpControllerDispatcher 将请求发送到Web API控制器。
我们可以向管道添加自定义处理程序。消息处理程序更适合处理Http Messages,因为Message Handler可以进行以下操作:
Text Code:
1.读取或修改请求标头。
2.为响应添加响应标头。
3.在请求到达控制器之前验证请求。
2.为响应添加响应标头。
3.在请求到达控制器之前验证请求。
此图显示了插入管道的两个自定义处理程序(Custom Handlers):

向MessageHandlers添加自定义Message Handler:
CSFramework.WebApi服务端框架自定义以下3个消息处理程序:
1. IP黑名单消息处理 IPBlacklistHandler
2. 接口访问限制流量 ThrottlingHandler
3. 添加监听接口访问器 RequestListenHandler
C# Code:
/// <summary>
/// 用户自定义消息配置
/// </summary>
public static class WebApiMessagesConfig
{
public static void Set(HttpConfiguration config, ILog2Database log)
{
//注意消息处理优先级别(顺序)!!!
//添加IP黑名单消息处理
if (WebApiSettings.Current.AllowIPBlacklistAccessDenial)
config.MessageHandlers.Add(new IPBlacklistHandler(log));
//添加接口访问限制流量
if (WebApiSettings.Current.AllowThrottling)
{
ThrottlingHandler throttling = new ThrottlingHandler()
{
Policy = new ThrottlePolicy(perSecond: 2, perMinute: 60, perHour: 600, perDay: 1500)
{
IpThrottling = true,//限制来自同IP请求的最大次数。如果在一分钟内,同样IP的客户端分别调用api/values和api/values/1两个接口, 那么调用api/values/1的请求会被拒绝掉。
EndpointThrottling = true// 如果配置了端点限流,同一秒内你也访问api/values/1了,请求将不会被拒绝,因为它们走的是不同的路由
},
Repository = new MemoryCacheRepository()
};
config.MessageHandlers.Add(throttling);//限流
}
//添加监听接口访问器
if (WebApiSettings.Current.AllowRequestListen)
config.MessageHandlers.Add(new RequestListenHandler(log));
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 用户自定义消息配置
/// </summary>
public static class WebApiMessagesConfig
{
public static void Set(HttpConfiguration config, ILog2Database log)
{
//注意消息处理优先级别(顺序)!!!
//添加IP黑名单消息处理
if (WebApiSettings.Current.AllowIPBlacklistAccessDenial)
config.MessageHandlers.Add(new IPBlacklistHandler(log));
//添加接口访问限制流量
if (WebApiSettings.Current.AllowThrottling)
{
ThrottlingHandler throttling = new ThrottlingHandler()
{
Policy = new ThrottlePolicy(perSecond: 2, perMinute: 60, perHour: 600, perDay: 1500)
{
IpThrottling = true,//限制来自同IP请求的最大次数。如果在一分钟内,同样IP的客户端分别调用api/values和api/values/1两个接口, 那么调用api/values/1的请求会被拒绝掉。
EndpointThrottling = true// 如果配置了端点限流,同一秒内你也访问api/values/1了,请求将不会被拒绝,因为它们走的是不同的路由
},
Repository = new MemoryCacheRepository()
};
config.MessageHandlers.Add(throttling);//限流
}
//添加监听接口访问器
if (WebApiSettings.Current.AllowRequestListen)
config.MessageHandlers.Add(new RequestListenHandler(log));
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0




扫一扫加微信:



版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网