@@ -17,7 +17,8 @@ namespace Microsoft.AspNetCore.Builder
17
17
/// </summary>
18
18
public static class UseMiddlewareExtensions
19
19
{
20
- private const string InvokeMethodName = "Invoke" ;
20
+ internal const string InvokeMethodName = "Invoke" ;
21
+ internal const string InvokeAsyncMethodName = "InvokeAsync" ;
21
22
22
23
private static readonly MethodInfo GetServiceInfo = typeof ( UseMiddlewareExtensions ) . GetMethod ( nameof ( GetService ) , BindingFlags . NonPublic | BindingFlags . Static ) ;
23
24
@@ -44,7 +45,7 @@ public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, Ty
44
45
{
45
46
if ( typeof ( IMiddleware ) . GetTypeInfo ( ) . IsAssignableFrom ( middleware . GetTypeInfo ( ) ) )
46
47
{
47
- // IMiddleware doesn't support passing args directly since it's
48
+ // IMiddleware doesn't support passing args directly since it's
48
49
// activated from the container
49
50
if ( args . Length > 0 )
50
51
{
@@ -58,27 +59,31 @@ public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, Ty
58
59
return app . Use ( next =>
59
60
{
60
61
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
+
62
67
if ( invokeMethods . Length > 1 )
63
68
{
64
- throw new InvalidOperationException ( Resources . FormatException_UseMiddleMutlipleInvokes ( InvokeMethodName ) ) ;
69
+ throw new InvalidOperationException ( Resources . FormatException_UseMiddleMutlipleInvokes ( InvokeMethodName , InvokeAsyncMethodName ) ) ;
65
70
}
66
71
67
72
if ( invokeMethods . Length == 0 )
68
73
{
69
- throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNoInvokeMethod ( InvokeMethodName ) ) ;
74
+ throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNoInvokeMethod ( InvokeMethodName , InvokeAsyncMethodName ) ) ;
70
75
}
71
76
72
77
var methodinfo = invokeMethods [ 0 ] ;
73
78
if ( ! typeof ( Task ) . IsAssignableFrom ( methodinfo . ReturnType ) )
74
79
{
75
- throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNonTaskReturnType ( InvokeMethodName , nameof ( Task ) ) ) ;
80
+ throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNonTaskReturnType ( InvokeMethodName , InvokeAsyncMethodName , nameof ( Task ) ) ) ;
76
81
}
77
82
78
83
var parameters = methodinfo . GetParameters ( ) ;
79
84
if ( parameters . Length == 0 || parameters [ 0 ] . ParameterType != typeof ( HttpContext ) )
80
85
{
81
- throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNoParameters ( InvokeMethodName , nameof ( HttpContext ) ) ) ;
86
+ throw new InvalidOperationException ( Resources . FormatException_UseMiddlewareNoParameters ( InvokeMethodName , InvokeAsyncMethodName , nameof ( HttpContext ) ) ) ;
82
87
}
83
88
84
89
var ctorArgs = new object [ args . Length + 1 ] ;
@@ -127,7 +132,7 @@ private static IApplicationBuilder UseMiddlewareInterface(IApplicationBuilder ap
127
132
128
133
try
129
134
{
130
- await middleware . Invoke ( context , next ) ;
135
+ await middleware . InvokeAsync ( context , next ) ;
131
136
}
132
137
finally
133
138
{
@@ -140,12 +145,12 @@ private static IApplicationBuilder UseMiddlewareInterface(IApplicationBuilder ap
140
145
private static Func < T , HttpContext , IServiceProvider , Task > Compile < T > ( MethodInfo methodinfo , ParameterInfo [ ] parameters )
141
146
{
142
147
// If we call something like
143
- //
148
+ //
144
149
// public class Middleware
145
150
// {
146
151
// public Task Invoke(HttpContext context, ILoggerFactory loggeryFactory)
147
152
// {
148
- //
153
+ //
149
154
// }
150
155
// }
151
156
//
0 commit comments