MSL.Dialogue.Pipeline
1.0.27
See the version list below for details.
dotnet add package MSL.Dialogue.Pipeline --version 1.0.27
NuGet\Install-Package MSL.Dialogue.Pipeline -Version 1.0.27
<PackageReference Include="MSL.Dialogue.Pipeline" Version="1.0.27" />
paket add MSL.Dialogue.Pipeline --version 1.0.27
#r "nuget: MSL.Dialogue.Pipeline, 1.0.27"
// Install MSL.Dialogue.Pipeline as a Cake Addin
#addin nuget:?package=MSL.Dialogue.Pipeline&version=1.0.27
// Install MSL.Dialogue.Pipeline as a Cake Tool
#tool nuget:?package=MSL.Dialogue.Pipeline&version=1.0.27
Dialogue
Dialogue is a request pipeline that supports middleware delegates and classes. It provides configuration, dependency injection, and middleware pipeline services for AWS Lambda functions or Azure functions.
References
Dialogue is based this article: How is the ASP.NET Core Middleware Pipeline Built - Steve Gorden, July 2020
Getting Started
If you're not familiar with middleware pipelines, Microsoft has a good primer on how middleware works in ASP.NET Core.
Installing
To install, use the following command: dotnet add package MSL.Dialogue.Pipeline
Usage
- Create an
IRequestHandlerBuilder<TRequest, TResponse>
by calling one of the staticRequestHandlerBuilder.New
methods. - Builder adds configuration providers for appsettings files, environment variables, command line args, and user secrets by default.
- Handle additional configuration scenarios through the
IConfigurationManager Configuration
property on the builder. - Register services with the
IServiceCollection Servics
property. - Use the
Build
method to create anIRequestRequestDelegate<TRequest, TResponse>
instance. - Configure the request delegate pipeline by calling the
Use
methods on the request handler. - Call the
Prepare
method on the request handler to compile the pipeline. If you don't callPrepare
, the pipeline will be compiled when the first request is processed. - Call the
InvokeAsync
method on the request handler to forward the request to the pipeline. - Within your middleware delegates, or
IMiddleware
implementations, always invokeNext
to pass the request context to the next delegate in the pipeline. - Always call
context.CancelationToken.ThrowIfCancellationRequested()
to check for cancellation requests before processing the request or invokingNext
. - To terminate, or "short circuit", the pipeline don't invoke
Next
. - In your terminal delegate, set the response value in the request context.
Samples
Projects
AWS Lambda (SQS)
- Dialogue.Samples.Lambda.SQS
- Dialogue.Samples.Lambda.SQS.Tests
- Dialogue.Samples.Lambda.APIGateway
- Dialogue.Samples.Lambda.APIGateway.Tests
Simplest Example - no config, no services, no middleware
In this sample, we create a request handler that does nothing with no configuration, no service registration, and no user-defined middleware. This is the simplest possible example.
var request = "Hello, World!";
var handler = RequestHandlerBuilder
.New<string, string>()
.Build();
var response = await handler.InvokeAsync(request);
Assert.True(String.IsNullOrEmpty(response));
Middleware Delegate Example
In this sample, we create a request handler with user-defined middleware that converts the request to uppercase.
var request = "Hello, World!";
var handler = RequestHandlerBuilder.New<string, string>()
.Build()
.Use(async (context, next) =>
{
context.CancelationToken.ThrowIfCancellationRequested();
context.Response = context.Request.ToUpperInvariant();
await next(context); // call next to pass the request context to next delegate in the pipeline
})
.Prepare();
var response = await handler.InvokeAsync(request);
Assert.Equal(request.ToUpperInvariant(), response);
Middleware Class Example
In this sample, we create a request handler with a user-defined IMiddleware
implementation that converts the request to lowercase.
First, we define the middleware class, which receives the next middleware delegate in the pipeline in its constructor. The middleware is responsible for calling the next delegate unless the middleware needs to short-circuiting the pipeline. An example short-circuit scenario might be a request validation middleware that returns an error response if the request is invalid.
IMiddleware implementations support constructor-based dependency injection. The next delegate must be the first argument in the contstructor.
internal sealed class ToLowerMiddleware(RequestMiddleware<string, string> next)
: IMiddleware<string, string>
{
private readonly RequestMiddleware<string, string> next = next
?? throw new ArgumentNullException(nameof(next));
public Task InvokeAsync(Context<string, string> context)
{
context.CancelationToken.ThrowIfCancellationRequested();
context.Response = context.Request.ToLowerInvariant();
return next(context); // call next to pass the request context to next delegate in the pipeline
}
}
Then we register it with the request handler.
var request = "Hello, World!";
var handler = RequestHandlerBuilder.New<string, string>()
.Build()
.Use<ToLowerMiddleware>()
.Prepare();
var response = await handler.InvokeAsync(request);
Assert.Equal(request.ToLowerInvariant(), response);
Builder Configure Example
Call the Configure
method on the builder to load configuration from appsettings.json
, environment variables, user secrets, etc.
Call Configure
before calling ConfigureServices
.
var builder = RequestHandlerBuilder.New<string, string>();
builder.Configuration
.AddInMemoryCollection(new Dictionary<string, string> { { "MyKey", "MyValue" } });
var handler = builder.Build();
Builder ConfigureServices Example
Call the ConfigureServices
method on the builder to register services with IServiceCollection
.
Call Configure
before calling ConfigureServices
.
var builder = RequestHandlerBuilder.New<string, string>();
builder.Services
.AddSingleton<IMyService, MyService>()
.AddSerilog();
var handler = builder.Build();
Void Response Example
For request handlers that don't return a response, use Void
as the response type.
var handler = RequestHandlerBuilder.New<string, Void>() // TResponse of type Void
.Build()
.Prepare();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Ulid (>= 1.3.3)
-
net7.0
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Ulid (>= 1.3.3)
-
net8.0
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Ulid (>= 1.3.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.