Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 2bdbbbf

Browse files
committed
UseMiddleware resolves InvokeAsync in addition to Invoke
1 parent b3b846c commit 2bdbbbf

File tree

5 files changed

+177
-100
lines changed

5 files changed

+177
-100
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/Extensions/UseMiddlewareExtensions.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace Microsoft.AspNetCore.Builder
1717
/// </summary>
1818
public static class UseMiddlewareExtensions
1919
{
20-
private const string InvokeMethodName = "Invoke";
20+
internal const string InvokeMethodName = "Invoke";
21+
internal const string InvokeAsyncMethodName = "InvokeAsync";
2122

2223
private static readonly MethodInfo GetServiceInfo = typeof(UseMiddlewareExtensions).GetMethod(nameof(GetService), BindingFlags.NonPublic | BindingFlags.Static);
2324

@@ -44,7 +45,7 @@ public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, Ty
4445
{
4546
if (typeof(IMiddleware).GetTypeInfo().IsAssignableFrom(middleware.GetTypeInfo()))
4647
{
47-
// IMiddleware doesn't support passing args directly since it's
48+
// IMiddleware doesn't support passing args directly since it's
4849
// activated from the container
4950
if (args.Length > 0)
5051
{
@@ -58,27 +59,31 @@ public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, Ty
5859
return app.Use(next =>
5960
{
6061
var methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public);
61-
var invokeMethods = methods.Where(m => string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal)).ToArray();
62+
var invokeMethods = methods.Where(m =>
63+
string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal)
64+
|| string.Equals(m.Name, InvokeAsyncMethodName, StringComparison.Ordinal)
65+
).ToArray();
66+
6267
if (invokeMethods.Length > 1)
6368
{
64-
throw new InvalidOperationException(Resources.FormatException_UseMiddleMutlipleInvokes(InvokeMethodName));
69+
throw new InvalidOperationException(Resources.FormatException_UseMiddleMutlipleInvokes(InvokeMethodName, InvokeAsyncMethodName));
6570
}
6671

6772
if (invokeMethods.Length == 0)
6873
{
69-
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoInvokeMethod(InvokeMethodName));
74+
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoInvokeMethod(InvokeMethodName, InvokeAsyncMethodName));
7075
}
7176

7277
var methodinfo = invokeMethods[0];
7378
if (!typeof(Task).IsAssignableFrom(methodinfo.ReturnType))
7479
{
75-
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNonTaskReturnType(InvokeMethodName, nameof(Task)));
80+
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNonTaskReturnType(InvokeMethodName, InvokeAsyncMethodName, nameof(Task)));
7681
}
7782

7883
var parameters = methodinfo.GetParameters();
7984
if (parameters.Length == 0 || parameters[0].ParameterType != typeof(HttpContext))
8085
{
81-
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoParameters(InvokeMethodName, nameof(HttpContext)));
86+
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoParameters(InvokeMethodName, InvokeAsyncMethodName, nameof(HttpContext)));
8287
}
8388

8489
var ctorArgs = new object[args.Length + 1];
@@ -127,7 +132,7 @@ private static IApplicationBuilder UseMiddlewareInterface(IApplicationBuilder ap
127132

128133
try
129134
{
130-
await middleware.Invoke(context, next);
135+
await middleware.InvokeAsync(context, next);
131136
}
132137
finally
133138
{
@@ -140,12 +145,12 @@ private static IApplicationBuilder UseMiddlewareInterface(IApplicationBuilder ap
140145
private static Func<T, HttpContext, IServiceProvider, Task> Compile<T>(MethodInfo methodinfo, ParameterInfo[] parameters)
141146
{
142147
// If we call something like
143-
//
148+
//
144149
// public class Middleware
145150
// {
146151
// public Task Invoke(HttpContext context, ILoggerFactory loggeryFactory)
147152
// {
148-
//
153+
//
149154
// }
150155
// }
151156
//

src/Microsoft.AspNetCore.Http.Abstractions/IMiddleware.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
84
using System.Threading.Tasks;
95

106
namespace Microsoft.AspNetCore.Http
@@ -20,6 +16,6 @@ public interface IMiddleware
2016
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
2117
/// <param name="next">The delegate representing the remaining middleware in the request pipeline.</param>
2218
/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>
23-
Task Invoke(HttpContext context, RequestDelegate next);
19+
Task InvokeAsync(HttpContext context, RequestDelegate next);
2420
}
2521
}

src/Microsoft.AspNetCore.Http.Abstractions/Properties/Resources.Designer.cs

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.AspNetCore.Http.Abstractions/Resources.resx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@
121121
<value>'{0}' is not available.</value>
122122
</data>
123123
<data name="Exception_UseMiddlewareNoInvokeMethod" xml:space="preserve">
124-
<value>No public '{0}' method found.</value>
124+
<value>No public '{0}' or '{1}' method found.</value>
125125
</data>
126126
<data name="Exception_UseMiddlewareNonTaskReturnType" xml:space="preserve">
127-
<value>'{0}' does not return an object of type '{1}'.</value>
127+
<value>'{0}' or '{1}' does not return an object of type '{2}'.</value>
128128
</data>
129129
<data name="Exception_UseMiddlewareNoParameters" xml:space="preserve">
130-
<value>The '{0}' method's first argument must be of type '{1}'.</value>
130+
<value>The '{0}' or '{1}' method's first argument must be of type '{2}'.</value>
131131
</data>
132132
<data name="Exception_UseMiddleMutlipleInvokes" xml:space="preserve">
133-
<value>Multiple public '{0}' methods are available.</value>
133+
<value>Multiple public '{0}' or '{1}' methods are available.</value>
134134
</data>
135135
<data name="Exception_PathMustStartWithSlash" xml:space="preserve">
136136
<value>The path in '{0}' must start with '/'.</value>

0 commit comments

Comments
 (0)