Skip to content

Commit 73c7dc9

Browse files
committed
Fix IMiddleware not able to take options
1 parent 721ad1f commit 73c7dc9

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Source/Boxed.AspNetCore/Middleware/HttpExceptionMiddleware.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,34 @@ namespace Boxed.AspNetCore.Middleware;
1111
/// The <see cref="HttpException"/> handling middleware.
1212
/// </summary>
1313
/// <seealso cref="IMiddleware" />
14-
public class HttpExceptionMiddleware : IMiddleware
14+
public class HttpExceptionMiddleware
1515
{
16+
private readonly RequestDelegate next;
1617
private readonly HttpExceptionMiddlewareOptions options;
1718

1819
/// <summary>
1920
/// Initializes a new instance of the <see cref="HttpExceptionMiddleware"/> class.
2021
/// </summary>
22+
/// <param name="next">The next middleware.</param>
2123
/// <param name="options">The options.</param>
22-
public HttpExceptionMiddleware(HttpExceptionMiddlewareOptions options) =>
24+
public HttpExceptionMiddleware(RequestDelegate next, HttpExceptionMiddlewareOptions options)
25+
{
26+
this.next = next;
2327
this.options = options;
28+
}
2429

25-
/// <inheritdoc/>
26-
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
30+
/// <summary>
31+
/// Request handling method.
32+
/// </summary>
33+
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
34+
/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>
35+
public async Task InvokeAsync(HttpContext context)
2736
{
2837
ArgumentNullException.ThrowIfNull(context);
29-
ArgumentNullException.ThrowIfNull(next);
3038

3139
try
3240
{
33-
await next.Invoke(context).ConfigureAwait(false);
41+
await this.next.Invoke(context).ConfigureAwait(false);
3442
}
3543
catch (HttpException httpException)
3644
{

Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ namespace Boxed.AspNetCore.Middleware;
1010
/// shortcuts and returns an error status code.
1111
/// </summary>
1212
/// <seealso cref="IMiddleware" />
13-
public class RequestCanceledMiddleware : IMiddleware
13+
public class RequestCanceledMiddleware
1414
{
1515
private readonly ILogger<RequestCanceledMiddleware> logger;
16+
private readonly RequestDelegate next;
1617
private readonly RequestCanceledMiddlewareOptions options;
1718

1819
/// <summary>
1920
/// Initializes a new instance of the <see cref="RequestCanceledMiddleware"/> class.
2021
/// </summary>
22+
/// <param name="next">The next middleware.</param>
2123
/// <param name="options">The middleware options.</param>
2224
/// <param name="logger">A logger.</param>
2325
public RequestCanceledMiddleware(
26+
RequestDelegate next,
2427
RequestCanceledMiddlewareOptions options,
2528
ILogger<RequestCanceledMiddleware> logger)
2629
{
30+
this.next = next;
2731
this.options = options;
2832
this.logger = logger;
2933
}
3034

31-
/// <inheritdoc/>
32-
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
35+
/// <summary>
36+
/// Request handling method.
37+
/// </summary>
38+
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
39+
/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>
40+
public async Task InvokeAsync(HttpContext context)
3341
{
3442
ArgumentNullException.ThrowIfNull(context);
35-
ArgumentNullException.ThrowIfNull(next);
3643

3744
try
3845
{
39-
await next(context).ConfigureAwait(false);
46+
await this.next(context).ConfigureAwait(false);
4047
}
4148
catch (OperationCanceledException operationCanceledException)
4249
when (operationCanceledException.CancellationToken == context.RequestAborted)

Tests/Boxed.AspNetCore.Test/Middleware/RequestCanceledMiddlewareTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public void InvokeAsync_NullNext_ThrowsArgumentNullException() =>
3232
public async Task InvokeAsync_RequestNotCanceled_RunsNextMiddlewareAsync()
3333
{
3434
await new RequestCanceledMiddleware(
35+
this.next,
3536
new RequestCanceledMiddlewareOptions(),
3637
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
37-
.InvokeAsync(this.context, this.next)
38+
.InvokeAsync(this.context)
3839
.ConfigureAwait(false);
3940

4041
Assert.Equal(200, this.context.Response.StatusCode);
@@ -52,9 +53,10 @@ public async Task InvokeAsync_OperationCanceledExceptionThrownNotCanceled_RunsNe
5253
await Assert
5354
.ThrowsAsync<OperationCanceledException>(() =>
5455
new RequestCanceledMiddleware(
56+
this.next,
5557
new RequestCanceledMiddlewareOptions(),
5658
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
57-
.InvokeAsync(this.context, this.next))
59+
.InvokeAsync(this.context))
5860
.ConfigureAwait(false);
5961
}
6062

@@ -67,9 +69,10 @@ public async Task InvokeAsync_RequestCanceled_Returns499ClientClosedRequestAsync
6769
this.next = x => Task.FromCanceled(cancellationTokenSource.Token);
6870

6971
await new RequestCanceledMiddleware(
72+
this.next,
7073
new RequestCanceledMiddlewareOptions(),
7174
new Mock<ILogger<RequestCanceledMiddleware>>().Object)
72-
.InvokeAsync(this.context, this.next)
75+
.InvokeAsync(this.context)
7376
.ConfigureAwait(false);
7477

7578
Assert.Equal(RequestCanceledMiddlewareOptions.ClientClosedRequest, this.context.Response.StatusCode);

0 commit comments

Comments
 (0)