-
Notifications
You must be signed in to change notification settings - Fork 13
Middleware pipeline
Typin uses middleware pipeline to execute commands. The pipeline starts just after parsing command input. By default it consists only of Typin middlewares that are always executed at the end of the pipeline, just before IMiddlewareExtensions.PipelineTermination
action.
Default/Internal Typin middleware pipeline structure:
ResolveCommandSchemaAndInstance
-> InitializeDirectives
-> ExecuteDirectivesSubpipeline
-> [Directives subpipeline] -> HandleSpecialOptions
-> BindInput
-> [User middlewares] -> ExecuteCommand
.
You can use a command to list all middlewares in the app in their execution order: source.
Default/Internal Typin middleware pipeline execution visualization:
==============================================================================================================================================
Middleware type name | Assembly
==============================================================================================================================================
Typin.Internal.Pipeline.ResolveCommandSchemaAndInstance | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.InitializeDirectives | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.ExecuteDirectivesSubpipeline | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.HandleSpecialOptions | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.BindInput | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.ExecuteCommand | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
<PipelineTermination> |
Typin.Internal.Pipeline.ExecuteCommand | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.BindInput | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.HandleSpecialOptions | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.ExecuteDirectivesSubpipeline | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.InitializeDirectives | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
Typin.Internal.Pipeline.ResolveCommandSchemaAndInstance | Typin, Version=3.0.2.0, Culture=neutral, PublicKeyToken=null
==============================================================================================================================================
To define a middleware, just create a new class that implements the IMiddleware interface:
public sealed class ExampleMiddleware : IMiddleware
{
public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken)
{
context.Console.Output.WriteLine("-- Before command execution --");
await next();
context.Console.Output.WriteLine("-- After command execution-- ");
}
}
To implement IMiddleware
, the class needs to define an HandleAsync
method. This is the method that gets by called Typin when the pipeline is executed.
It is also possible to using dependency injection in IMiddleware implementation class, e.g.,
public sealed class ExampleMiddleware : IMiddleware
{
private readonly ILogger _logger;
public ExampleMiddleware (ILogger<ExampleMiddleware> logger)
{
_logger = logger;
}
public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken)
{
context.Console.Output.WriteLine("-- Before command execution --");
_logger.LogInformation("Executing custom middleware");
await next();
_logger.LogInformation("Finished executing custom middleware");
_console.Output.WriteLine("-- After command execution-- ");
}
}
Getting started
Advanced features
- Reporting errors
- Exception handling
- Metadata and startup message
- Graceful cancellation
- Dependency injection
- Middleware pipeline
- Environment variables
Utilities
Tests
Misc