Skip to content

Commit 2f4558c

Browse files
madsbachajeremydmiller
authored andcommitted
fix: add missing code-snippet in middleware.md
Added code snippet for applying middleware programmatically to an HTTP endpoint, specifically sample `sample_applying_middleware_programmatically_to_one_chain`.
1 parent cb0fcda commit 2f4558c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

docs/guide/http/middleware.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,31 @@ public class StopwatchMiddleware
150150
And you want to apply it to a single HTTP endpoint without having to dirty your hands with an attribute. You can use that naming
151151
convention up above like so:
152152

153-
sample: sample_applying_middleware_programmatically_to_one_chain
153+
<!-- snippet: sample_applying_middleware_programmatically_to_one_chain -->
154+
<a id='snippet-sample_applying_middleware_programmatically_to_one_chain'></a>
155+
```cs
156+
public class MeasuredEndpoint
157+
{
158+
// The signature is meaningful here
159+
public static void Configure(HttpChain chain)
160+
{
161+
// Call this method before the normal endpoint
162+
chain.Middleware.Add(MethodCall.For<StopwatchMiddleware>(x => x.Before()));
163+
164+
// Call this method after the normal endpoint
165+
chain.Postprocessors.Add(MethodCall.For<StopwatchMiddleware>(x => x.Finally(null, null)));
166+
}
167+
168+
[WolverineGet("/timed")]
169+
public async Task<string> Get()
170+
{
171+
await Task.Delay(100.Milliseconds());
172+
return "how long did I take?";
173+
}
174+
}
175+
```
176+
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/MiddlewareEndpoints.cs#L30-L52' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_applying_middleware_programmatically_to_one_chain' title='Start of snippet'>anchor</a></sup>
177+
<!-- endSnippet -->
154178

155179

156180
## Apply Middleware by Policy

0 commit comments

Comments
 (0)