Interaction Hooks Overview
Interaction hooks are server-side extension points that run at key moments in the interaction lifecycle. They enable cross-cutting concerns — audit, validation, rate limiting, enrichment — without modifying the core pipeline.
What Are Interaction Hooks?
EdgeInteract hooks extend the IInteractionHook interface and are registered in the DI container. The pipeline invokes all registered hooks at the appropriate lifecycle points. Hooks can inspect, modify, or block interactions without being coupled to application code.
Hook Interface
public interface IInteractionHook
{
/// Runs before the InteractionRequest is delivered via EdgeStream.
/// Throw InteractionBlockedException to prevent delivery.
Task OnBeforePublish(InteractionRequest request, CancellationToken ct);
/// Runs after an InteractionResponse is received and validated.
/// Cannot block the cycle, but can perform side effects.
Task OnAfterRespond(
InteractionRequest request,
InteractionResponse response,
CancellationToken ct);
}
Built-in Hooks
| Hook | Type | Purpose | Required? |
|---|---|---|---|
AuditInteractionHook | Both | Logs every request/response pair to the audit store | Recommended |
RateLimitInteractionHook | Pre-send | Prevents flooding a user with too many interactions | Recommended |
ResponseValidationHook | Post-receive | Validates response outcome and data schema per type | Always enabled |
MetricsInteractionHook | Both | Emits OTel metrics for each lifecycle event | Optional |
Hook Lifecycle Points
OnBeforePublish
Runs after request validation but before EdgeStream delivery. All registered hooks run in sequence. Any hook can throw InteractionBlockedException to abort publication.
[Interaction Lifecycle — Delivery → Display → Respond]
No hooks run during the client-side stages. The pipeline is silent until the response arrives.
OnAfterRespond
Runs after the response arrives and the response validation hook approves it. Cannot block. All hooks run in sequence. Exceptions are logged but do not fail the interaction cycle.
Registering Hooks
// Program.cs
builder.Services.AddEdgeInteract(options => {
options.DefaultTimeoutMs = 86_400_000;
});
// Register built-in hooks
builder.Services.AddInteractionHook<AuditInteractionHook>();
builder.Services.AddInteractionHook<RateLimitInteractionHook>();
builder.Services.AddInteractionHook<MetricsInteractionHook>();
// Register custom hooks
builder.Services.AddInteractionHook<MyEnrichmentHook>();
builder.Services.AddInteractionHook<MyComplianceHook>();