DcsvIo.D2.Handler
0.2.0
dotnet add package DcsvIo.D2.Handler --version 0.2.0
NuGet\Install-Package DcsvIo.D2.Handler -Version 0.2.0
<PackageReference Include="DcsvIo.D2.Handler" Version="0.2.0" />
<PackageVersion Include="DcsvIo.D2.Handler" Version="0.2.0" />
<PackageReference Include="DcsvIo.D2.Handler" />
paket add DcsvIo.D2.Handler --version 0.2.0
#r "nuget: DcsvIo.D2.Handler, 0.2.0"
#:package DcsvIo.D2.Handler@0.2.0
#addin nuget:?package=DcsvIo.D2.Handler&version=0.2.0
#tool nuget:?package=DcsvIo.D2.Handler&version=0.2.0
DcsvIo.D2.Handler
BaseHandler<TSelf, TInput, TOutput> is the abstract base that every handler inherits (CQRS handlers, repo handlers,
messaging consumers, scheduled jobs, anything handler-shaped). It provides a per-handler scope pre-check, an
OpenTelemetry activity, handler metrics, a log scope, a stopwatch and a try/catch that wraps the subclass's
ExecuteAsync and catches every exception. The sibling package DcsvIo.D2.Handler.Repo adds a mapping of EF and
PostgreSQL exceptions to D2Result.
Auth middleware (HTTP, gRPC, AMQP) validates JWT signature, expiry, audience and fingerprint binding at the transport level, before the handler runs. When a handler executes, the host service has already validated the bearer token.
Install
dotnet add package DcsvIo.D2.Handler
PII safety: setup required before deploying any handler
BaseHandler logs handler input via Serilog destructuring ({@Input}) at Debug level, gated by
HandlerOptions.LogInput (default true). Without redaction wiring, any field on the input that contains PII (email,
phone, name, address, raw IP, etc.) appears verbatim in logs whenever Debug-level logging is enabled.
Every consuming service must register the [RedactData]-aware Serilog destructuring policy at startup. Hosts compose
this themselves with DcsvIo.D2.Logging and Serilog wiring; BaseHandler does not require a host-wide aggregator. If
you bootstrap logging manually, register the policy yourself.
If you cannot guarantee that the redaction policy is in place (for example in a one-off tool or an early bootstrap
path), set LogInput = false in DefaultOptions for every handler that touches PII. Prefer registering the redaction
policy once at host startup over relying on per-handler LogInput = false.
Public API
| Type | Role |
|---|---|
BaseHandler<TSelf, TInput, TOutput> |
Abstract base class. Virtual HandleAsync (entry point), non-virtual RunCorePipelineAsync (observability and try/catch) and abstract ExecuteAsync (subclass logic) |
HandlerContext<T> |
Typed-logger context. Open-generic registration via AddD2Handler |
HandlerTelemetry |
Static OpenTelemetry primitives: an ActivitySource, a Meter and instruments (counters d2.handler.invoked / succeeded / failed and histogram d2.handler.duration) |
AddD2Handler |
DI extension that registers the open-generic HandlerContext<T> |
A null handler input returns D2Result.ValidationFailed before the scope checks, LogInput and ExecuteAsync, and
does not throw ArgumentNullException. TInput : notnull states the same rule in the type system.
BaseHandler shape
public abstract class BaseHandler<TSelf, TInput, TOutput> : IHandler<TInput, TOutput>
where TSelf : BaseHandler<TSelf, TInput, TOutput>
where TInput : notnull
{
protected BaseHandler(HandlerContext<TSelf> context);
protected IHandlerContext Context { get; }
protected virtual HandlerOptions DefaultOptions { get; }
public virtual ValueTask<D2Result<TOutput?>> HandleAsync(
TInput input, CancellationToken ct = default, HandlerOptions? options = null);
protected ValueTask<(D2Result<TOutput?> Result, Exception? CapturedException)> RunCorePipelineAsync(
TInput input, CancellationToken ct, HandlerOptions? options);
protected abstract ValueTask<D2Result<TOutput?>> ExecuteAsync(TInput input, CancellationToken ct);
}
TInput : notnull matches IHandler: optional or absent values are members of the input type, never a nullable
whole-input type argument.
RunCorePipelineAsync returns the captured exception alongside the result so subclasses for EF Core (BaseRepoHandler)
can remap typed exceptions to D2Result failure codes from their own overridden HandleAsync.
Host requirement
HandleAsync awaits without ConfigureAwait(false), so the package is supported only in a host with no
SynchronizationContext - which is what an ASP.NET Core host runs under. In a host that installs one, every
continuation marshals back to it and a caller that blocks on HandleAsync deadlocks.
HandleAsync flow
- Resolve options: per-call →
DefaultOptions→ platform defaults - Null input gate:
input is null→ incrementsHandlerTelemetry.SR_Invoked+SR_Failed, logs atWarning(event id 8), then returnsD2Result.ValidationFailed(before scope, LogInput,ExecuteAsync) - Scope pre-check (Require): if
ScopeRequirementis non-null with a non-emptyScopesset, checksContext.Request.Scopesusing the declaredHandlerScopeMatch-Any(at least one overlap) orAll(every scope present); skip whenScopeRequirementis null orScopesis empty; mismatch → incrementsHandlerTelemetry.SR_Invoked+SR_Failed, skips activity span and duration recording, returnsD2Result.Forbidden - Composition elevate (Elevate): a non-empty
ElevateScopesrequires a non-emptyScopeRequirement; the pairing is refused withD2Result.Forbiddenahead of the scope check when the requirement is null or has no scopes, because elevation without one would grant the elevated scopes to whatever caller reached the handler. Otherwise the ambient request is cast toMutableRequestContext(refused withD2Result.Forbidden, failing closed, when it is not mutable) and the scopes are pushed through the internalScopeElevation.Pushlease for the Execute window. Null/emptyElevateScopesis a no-op. Nested child handlers see the elevated ambient set for Require. Both refusals are handler declaration or composition faults and not caller denials, so each logs atError(event ids 9 and 10) with the handler name; an ordinary scope denial at step 3 logs neither. - Activity start:
HandlerTelemetry.SR_ActivitySource.StartActivity(handlerName). Tags emitted (when present):- Always:
d2.handler.name - When user identity present:
d2.user_id,d2.org_id,d2.org_type,d2.org_role - When impersonating:
d2.impersonating,d2.impersonation_kind,d2.impersonator_id,d2.impersonator_org_id,d2.impersonator_org_type,d2.impersonator_org_role
- Always:
- Counter:
HandlerTelemetry.SR_Invoked.Add(1, handlerNameTag) ExecuteAsyncinside try/catch (under elevate lease when set)- Stopwatch + threshold logging: warn at
SlowThreshold, error atCriticalThreshold - Result handling:
- Success →
Succeededcounter + debug log - Failure →
Failedcounter + debug log OperationCanceledExceptionwith thectpassed toHandleAsynccanceled →D2Result.Canceled+ info log (intentional caller cancellation)OperationCanceledExceptionwithout thatctcanceled →D2Result.ServiceUnavailable+ warn log (downstream timeout - HttpClient timeout, SQL command timeout, internal handler watchdog)- Other exception →
D2Result.UnhandledException+ error log + activity status set
- Success →
- Duration:
HandlerTelemetry.SR_Duration.Record(elapsedMs, handlerNameTag)always - Elevate restore: lease dispose removes exactly the scopes that lease added, so overlapping leases over one request combine - a scope stays in force until the last lease that added it is disposed, and a scope the ambient set already contained is never removed
The pipeline injects TraceId automatically into every emitted D2Result from Context.Request.TraceId.
Require vs Elevate vs Strip vs S2S
| Concept | Meaning |
|---|---|
| Require | Who may enter this op (ScopeRequirement vs ambient Scopes) |
| Elevate | Composition scopes for a lease window only, declared as HandlerOptions.ElevateScopes and applied by BaseHandler |
| Strip | Untrusted external HTTP JWT/session presentation never retains internal.* ambient scopes |
| S2S | Workload token on gRPC+mTLS may include allow-listed internal.*; not a substitute for in-process elevate |
ScopeElevation is internal to this assembly and is not part of the published surface.
HandlerOptions.ElevateScopes is the whole elevation contract a consumer of this package has: declare the scopes on the
handler and nest plain HandleAsync. No PublicAPI entry exposes Push, so its shape may change in any release without
a major bump. A consumer that needs elevation outside a handler should ask for a public seam.
One recorded exception calls Push from outside this assembly. The project file grants InternalsVisibleTo to a
private host assembly and its test assembly for scope elevation in one middleware call, which establishes an anonymous
session before a handler exists to declare ElevateScopes. The assemblies are not strong-named, so the grant is not a
security boundary: any assembly can take that name. The grant adds no published surface and no compatibility promise
for Push.
Why distinguish caller-canceled from downstream-timeout
A downstream timeout, such as HttpClient.Timeout firing, surfaces as an OperationCanceledException whose token is
the timeout's internal token and not the ct passed to HandleAsync. UnhandledException (HTTP 500) would imply a bug
in the handler's own code. ServiceUnavailable (HTTP 503) correctly signals that a dependency the handler needed is not
responding, which is the right HTTP semantic and the right operational signal.
DI registration
services.AddD2Handler(); // registers open-generic HandlerContext<T> as Transient
// Per-handler registration (typically in the app layer AddXxxApp extension):
services.AddTransient<IGetUserById, GetUserById>();
AddD2Handler does not register IRequestContext, which is transport-specific. Each consuming transport stack must
construct the per-request IRequestContext and put it into the DI scope before any handler resolves:
- HTTP / gRPC.AspNetCore: the consuming service's startup wires HTTP middleware that builds
IRequestContextfrom the validated bearer token and the ambient request data - RabbitMQ consumer: the consuming service's consumer pipeline builds
IRequestContextfrom the AMQP frame headers and the decrypted body
Tests supply a MutableRequestContext through a test fixture builder.
Telemetry instruments
| Instrument | Type | Unit | Description |
|---|---|---|---|
d2.handler.invoked |
Counter (long) | {calls} |
Handler invocations attempted |
d2.handler.succeeded |
Counter (long) | {calls} |
Handler invocations that returned Success == true |
d2.handler.failed |
Counter (long) | {calls} |
Handler invocations that returned Success == false or threw |
d2.handler.duration |
Histogram (double) | ms |
Handler invocation wall-clock duration |
All instruments are tagged with d2.handler.name = typeof(TSelf).Name. Hosts register the OpenTelemetry SDK via
DcsvIo.D2.Telemetry (AddD2Telemetry) so MeterProvider / TracerProvider capture the DcsvIo.D2.Handler source.
Subclassing pattern
public sealed class GetUserById(HandlerContext<GetUserById> context, IUserRepo repo)
: BaseHandler<GetUserById, GetUserByIdInput, UserDto>, IGetUserById
{
protected override HandlerOptions DefaultOptions => new()
{
ScopeRequirement = new ScopeRequirement(HandlerScopeMatch.Any, new HashSet<string>(StringComparer.Ordinal) { Scopes.Self.Read }),
SlowThreshold = TimeSpan.FromMilliseconds(50),
};
protected override async ValueTask<D2Result<UserDto?>> ExecuteAsync(
GetUserByIdInput input, CancellationToken ct)
{
var user = await repo.FindAsync(input.Id, ct);
if (user is null)
{
return D2Result<UserDto?>.NotFound();
}
return D2Result<UserDto?>.Ok(user.ToDto());
}
}
Handler primary-constructor parameters do not take the r_ prefix (an exception to the standard naming convention).
Dependencies
DcsvIo.D2.Handler.Abstractions-IHandler,HandlerOptions,IHandlerContextDcsvIo.D2.Context.Abstractions- request contextDcsvIo.D2.Result- result typeMicrosoft.Extensions.DependencyInjection.Abstractions/Microsoft.Extensions.Logging.Abstractions
Related packages
DcsvIo.D2.Handler.Abstractions-IHandler+HandlerOptions+IHandlerContextDcsvIo.D2.Handler.Repo- subclass that remaps EF and PostgreSQL exceptionsDcsvIo.D2.Telemetry- OpenTelemetry SDK wiring that captures handler instruments
Recommended layout: one handler folder per operation (Application/Handlers/{Commands,Queries}/<Op>/) with
primary-constructor handlers (constructor parameters do not take the r_ field prefix).
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- DcsvIo.D2.Context.Abstractions (= 0.3.21)
- DcsvIo.D2.Handler.Abstractions (= 0.2.20)
- DcsvIo.D2.Result (= 0.5.8)
- JetBrains.Annotations (>= 2025.2.4)
- Microsoft.Extensions.DependencyInjection (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Logging (>= 10.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Options (>= 10.0.7)
- Microsoft.IdentityModel.Tokens (>= 8.18.0)
- NodaTime (>= 3.2.2)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on DcsvIo.D2.Handler:
| Package | Downloads |
|---|---|
|
DcsvIo.D2.Messaging.Abstractions
Transport-agnostic messaging abstractions for D2 - the [MqPub] / [MqSub] vocabulary, the message-bus contract, and DLQ failure-metadata wire shapes. |
|
|
DcsvIo.D2.Messaging.RabbitMq
Default RabbitMQ implementation of the D2 messaging abstractions - publishing, subscribing, encryption frames, and dead-letter handling. |
|
|
DcsvIo.D2.Handler.Repo
EF-flavored BaseRepoHandler for D2 - converts database exceptions captured during execution into typed D2Result failures. |
|
|
DcsvIo.D2.Telemetry
OpenTelemetry SDK setup for D2 - traces, metrics, logs, OTLP exporters, an IP-restricted Prometheus endpoint, and aggregation of every shared library's ActivitySource and Meter. |
GitHub repositories
This package is not used by any popular GitHub repositories.