@@ -28,8 +28,8 @@ public static void UseProxies(this IApplicationBuilder app)
2828 var attribute = method . GetCustomAttributes ( typeof ( ProxyRouteAttribute ) , false ) . First ( ) as ProxyRouteAttribute ;
2929 var parameters = method . GetParameters ( ) ;
3030
31- if ( ! ( method . ReturnType == typeof ( Task < string > ) ) )
32- throw new InvalidOperationException ( $ "Proxied generator method ({ name } ) must return a Task<string>.") ;
31+ if ( method . ReturnType != typeof ( Task < string > ) && method . ReturnType != typeof ( string ) )
32+ throw new InvalidOperationException ( $ "Proxied generator method ({ name } ) must return a ` Task<string>` or `string` .") ;
3333
3434 if ( ! method . IsStatic )
3535 throw new InvalidOperationException ( $ "Proxied generator method ({ name } ) must be static.") ;
@@ -49,7 +49,12 @@ public static void UseProxies(this IApplicationBuilder app)
4949 }
5050 } ) ;
5151
52- return method . Invoke ( null , castedArgs . ToArray ( ) ) as Task < string > ;
52+ // Make sure to always return a `Task<string>`, but allow methods that just return a `string`.
53+
54+ if ( method . ReturnType == typeof ( Task < string > ) )
55+ return method . Invoke ( null , castedArgs . ToArray ( ) ) as Task < string > ;
56+
57+ return Task . FromResult ( method . Invoke ( null , castedArgs . ToArray ( ) ) as string ) ;
5358 } ) ;
5459 }
5560 }
@@ -59,16 +64,16 @@ public static void UseProxies(this IApplicationBuilder app)
5964 /// </summary>
6065 /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
6166 /// <param name="endpoint">The local route endpoint.</param>
62- /// <param name="getProxiedAddress">A functor which returns the address to which the request is proxied.</param>
63- /// <param name="onFailure">A catch all for failures.</param>
64- public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < IDictionary < string , object > , Task < string > > getProxiedAddress , Func < HttpContext , Exception , Task > onFailure = null )
67+ /// <param name="getProxiedAddress">A lambda { (context, args) => Task[string] } which returns the address to which the request is proxied.</param>
68+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => Task } .</param>
69+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < HttpContext , IDictionary < string , object > , Task < string > > getProxiedAddress , Func < HttpContext , Exception , Task > onFailure = null )
6570 {
6671 app . UseRouter ( builder => {
6772 builder . MapMiddlewareRoute ( endpoint , proxyApp => {
6873 proxyApp . Run ( async context => {
6974 try
7075 {
71- var proxiedAddress = await getProxiedAddress ( context . GetRouteData ( ) . Values . ToDictionary ( v => v . Key , v => v . Value ) ) . ConfigureAwait ( false ) ;
76+ var proxiedAddress = await getProxiedAddress ( context , context . GetRouteData ( ) . Values . ToDictionary ( v => v . Key , v => v . Value ) ) . ConfigureAwait ( false ) ;
7277 var proxiedResponse = await context . SendProxyHttpRequest ( proxiedAddress ) . ConfigureAwait ( false ) ;
7378
7479 await context . CopyProxyHttpResponse ( proxiedResponse ) . ConfigureAwait ( false ) ;
@@ -89,5 +94,91 @@ public static void UseProxy(this IApplicationBuilder app, string endpoint, Func<
8994 } ) ;
9095 } ) ;
9196 }
97+
98+ #region UseProxy Overloads
99+
100+ /// <summary>
101+ /// Middleware which creates an ad hoc proxy over a specified endpoint.
102+ /// </summary>
103+ /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
104+ /// <param name="endpoint">The local route endpoint.</param>
105+ /// <param name="getProxiedAddress">A lambda { (args) => Task[string] } which returns the address to which the request is proxied.</param>
106+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => Task }.</param>
107+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < IDictionary < string , object > , Task < string > > getProxiedAddress , Func < HttpContext , Exception , Task > onFailure = null )
108+ {
109+ Func < HttpContext , IDictionary < string , object > , Task < string > > gpa = ( context , args ) => getProxiedAddress ( args ) ;
110+
111+ UseProxy ( app , endpoint , gpa , onFailure ) ;
112+ }
113+
114+ /// <summary>
115+ /// Middleware which creates an ad hoc proxy over a specified endpoint.
116+ /// </summary>
117+ /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
118+ /// <param name="endpoint">The local route endpoint.</param>
119+ /// <param name="getProxiedAddress">A lambda { () => Task[string] } which returns the address to which the request is proxied.</param>
120+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => Task }.</param>
121+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < Task < string > > getProxiedAddress , Func < HttpContext , Exception , Task > onFailure = null )
122+ {
123+ Func < HttpContext , IDictionary < string , object > , Task < string > > gpa = ( context , args ) => getProxiedAddress ( ) ;
124+
125+ UseProxy ( app , endpoint , gpa , onFailure ) ;
126+ }
127+
128+ /// <summary>
129+ /// Middleware which creates an ad hoc proxy over a specified endpoint.
130+ /// </summary>
131+ /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
132+ /// <param name="endpoint">The local route endpoint.</param>
133+ /// <param name="getProxiedAddress">A lambda { (context, args) => string } which returns the address to which the request is proxied.</param>
134+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => void }.</param>
135+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < HttpContext , IDictionary < string , object > , string > getProxiedAddress , Action < HttpContext , Exception > onFailure = null )
136+ {
137+ Func < HttpContext , IDictionary < string , object > , Task < string > > gpa = ( context , args ) => Task . FromResult ( getProxiedAddress ( context , args ) ) ;
138+
139+ Func < HttpContext , Exception , Task > of = null ;
140+ if ( onFailure != null )
141+ of = ( context , e ) => { onFailure ( context , e ) ; return Task . FromResult ( 0 ) ; } ;
142+
143+ UseProxy ( app , endpoint , gpa , of ) ;
144+ }
145+
146+ /// <summary>
147+ /// Middleware which creates an ad hoc proxy over a specified endpoint.
148+ /// </summary>
149+ /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
150+ /// <param name="endpoint">The local route endpoint.</param>
151+ /// <param name="getProxiedAddress">A lambda { (args) => string } which returns the address to which the request is proxied.</param>
152+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => void }.</param>
153+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < IDictionary < string , object > , string > getProxiedAddress , Action < HttpContext , Exception > onFailure = null )
154+ {
155+ Func < HttpContext , IDictionary < string , object > , Task < string > > gpa = ( context , args ) => Task . FromResult ( getProxiedAddress ( args ) ) ;
156+
157+ Func < HttpContext , Exception , Task > of = null ;
158+ if ( onFailure != null )
159+ of = ( context , e ) => { onFailure ( context , e ) ; return Task . FromResult ( 0 ) ; } ;
160+
161+ UseProxy ( app , endpoint , gpa , of ) ;
162+ }
163+
164+ /// <summary>
165+ /// Middleware which creates an ad hoc proxy over a specified endpoint.
166+ /// </summary>
167+ /// <param name="app">The ASP.NET <see cref="IApplicationBuilder"/>.</param>
168+ /// <param name="endpoint">The local route endpoint.</param>
169+ /// <param name="getProxiedAddress">A lambda { () => string } which returns the address to which the request is proxied.</param>
170+ /// <param name="onFailure">A lambda to handle proxy failures { (context, exception) => void }.</param>
171+ public static void UseProxy ( this IApplicationBuilder app , string endpoint , Func < string > getProxiedAddress , Action < HttpContext , Exception > onFailure = null )
172+ {
173+ Func < HttpContext , IDictionary < string , object > , Task < string > > gpa = ( context , args ) => Task . FromResult ( getProxiedAddress ( ) ) ;
174+
175+ Func < HttpContext , Exception , Task > of = null ;
176+ if ( onFailure != null )
177+ of = ( context , e ) => { onFailure ( context , e ) ; return Task . FromResult ( 0 ) ; } ;
178+
179+ UseProxy ( app , endpoint , gpa , of ) ;
180+ }
181+
182+ #endregion
92183 }
93184}
0 commit comments