JH.SimplePipeline
1.1.1
dotnet add package JH.SimplePipeline --version 1.1.1
NuGet\Install-Package JH.SimplePipeline -Version 1.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JH.SimplePipeline" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JH.SimplePipeline" Version="1.1.1" />
<PackageReference Include="JH.SimplePipeline" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JH.SimplePipeline --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JH.SimplePipeline, 1.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JH.SimplePipeline@1.1.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JH.SimplePipeline&version=1.1.1
#tool nuget:?package=JH.SimplePipeline&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
简单快速创建一个上下文管道,类似于Asp.net 中的中间件管道,可以在开发中对文件解耦,是代码调理更清晰
使用方式
// 创建管道构建者
// 构建自己的上下文类型,会在整个管道中进行流转
// skipOption 选项会在管道开始流转时执行一次,如果返回 true ,后续管道中间件不会被执行(包括 DeferLayer)
var builder = new SimplePipelineBuilder<AntiCrawlerContext>(serviceProvider, async context => IfIgnore(context.HttpContext));
// 添加管道切面层
// 和 aspnet 中的中间件管道一样,会按照切面添加的顺序依次调用
builder
.UseLayer<RateLimitLayer>()
.UseLayer<UserRuleLayer>()
.UseLayer<DeferLayer1>()
.UseLayer<DeferLayer2>();
// 创建管道
SimplePipeline<AntiCrawlerContext> pipeline = builder.Build();
// 调用
var validateContext = ...
await pipeline.ExecuteAsync(validateContext);
示例
创建上下文:
public class AntiCrawlerContext : SimpleContext
{
public AntiCrawlerContext(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public int UserId { get; set; }
public Dictionary<string, object> Items { get; set; }
}
创建 IgnoreLayer 层,该类必须包含一个 InvokeAsync 方法(支持依赖注入),和有参构造函数,
public class IgnoreLayer
{
private readonly SimpleRequestDelegate<AntiCrawlerContext> _next;
public IgnoreLayer(SimpleRequestDelegate<AntiCrawlerContext> next)
{
_next = next;
}
/// <summary>
/// 每个切面必要要有改方法,否则会报错
/// </summary>
/// <param name="context"></param>
/// <param name="antiCrawlerService"></param>
public async Task InvokeAsync(AntiCrawlerContext context, IAntiCrawlerService antiCrawlerService)
{
// 该切面的一些负责验证逻辑
// ...
if (await antiCrawlerService.IsPassed(context.UserId))
{
await _next(context);
return;
}
// 如果没有执行 _next ,后去切面不再流转
}
}
创建收尾层
凡是标记了 [DeferLayer] 特性的切面层,不管其他层有没有流转,该层的 InvokeAsync 方法都会被调用,用来执行一些收尾操作,也遵循先添加先调用,后添加后调用的原则。 DeferLayer 层不需要有参构造函数,因为管道会自动流转。
[DeferLayer]
public class DeferLayer1
{
public async Task InvokeAsync(AntiCrawlerContext context)
{
// 收尾操作
// ...
}
}
[DeferLayer]
public class DeferLayer2
{
public async Task InvokeAsync(AntiCrawlerContext context)
{
// 收尾操作
// ...
}
}
InvokeAsync内如果没有调用next方法,后续的非[DeferLayer]中间件都不会调用。- 标记了
[DeferLayer]特性的中间件称为 Defer 中间件,不管前面有多少个中间件,或者执行怎样的操作,它们的InvokeAsync都会被调用,主要用来执行一些重制状态的操作。 - 因为上下文和
HttpContext上下文出自同一个Scope,在流转的过程中,可以存储在本次请求中获得各种状态特征值,也可以获得和 aspnet 原生DI中的各种已注册服务。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
优化是否跳过执行层选项