44using System . Diagnostics ;
55using System . Linq ;
66using System . Linq . Expressions ;
7+ using System . Net . Http ;
78using System . Reflection ;
89using System . Security . Claims ;
910using System . Text ;
@@ -63,14 +64,16 @@ public static partial class RequestDelegateFactory
6364 private static readonly BinaryExpression TempSourceStringNotNullExpr = Expression . NotEqual ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
6465 private static readonly BinaryExpression TempSourceStringNullExpr = Expression . Equal ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
6566
67+ private static readonly AcceptsMetadata DefaultAcceptsMetadata = new ( new [ ] { "application/json" } ) ;
68+
6669 /// <summary>
6770 /// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="action"/>.
6871 /// </summary>
6972 /// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
7073 /// <param name="options">The <see cref="RequestDelegateFactoryOptions"/> used to configure the behavior of the handler.</param>
71- /// <returns>The <see cref="RequestDelegate "/>.</returns>
74+ /// <returns>The <see cref="RequestDelegateResult "/>.</returns>
7275#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
73- public static RequestDelegate Create ( Delegate action , RequestDelegateFactoryOptions ? options = null )
76+ public static RequestDelegateResult Create ( Delegate action , RequestDelegateFactoryOptions ? options = null )
7477#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
7578 {
7679 if ( action is null )
@@ -84,12 +87,15 @@ public static RequestDelegate Create(Delegate action, RequestDelegateFactoryOpti
8487 null => null ,
8588 } ;
8689
87- var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , options , targetExpression ) ;
88-
89- return httpContext =>
90+ var factoryContext = new FactoryContext
9091 {
91- return targetableRequestDelegate ( action . Target , httpContext ) ;
92+ ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
9293 } ;
94+
95+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , options , factoryContext , targetExpression ) ;
96+
97+ return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( action . Target , httpContext ) , factoryContext . Metadata ) ;
98+
9399 }
94100
95101 /// <summary>
@@ -100,7 +106,7 @@ public static RequestDelegate Create(Delegate action, RequestDelegateFactoryOpti
100106 /// <param name="options">The <see cref="RequestDelegateFactoryOptions"/> used to configure the behavior of the handler.</param>
101107 /// <returns>The <see cref="RequestDelegate"/>.</returns>
102108#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
103- public static RequestDelegate Create ( MethodInfo methodInfo , Func < HttpContext , object > ? targetFactory = null , RequestDelegateFactoryOptions ? options = null )
109+ public static RequestDelegateResult Create ( MethodInfo methodInfo , Func < HttpContext , object > ? targetFactory = null , RequestDelegateFactoryOptions ? options = null )
104110#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
105111 {
106112 if ( methodInfo is null )
@@ -113,31 +119,30 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
113119 throw new ArgumentException ( $ "{ nameof ( methodInfo ) } does not have a declaring type.") ;
114120 }
115121
122+ var factoryContext = new FactoryContext
123+ {
124+ ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
125+ } ;
126+
116127 if ( targetFactory is null )
117128 {
118129 if ( methodInfo . IsStatic )
119130 {
120- var untargetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , targetExpression : null ) ;
131+ var untargetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , factoryContext , targetExpression : null ) ;
121132
122- return httpContext =>
123- {
124- return untargetableRequestDelegate ( null , httpContext ) ;
125- } ;
133+ return new RequestDelegateResult ( httpContext => untargetableRequestDelegate ( null , httpContext ) , factoryContext . Metadata ) ;
126134 }
127135
128136 targetFactory = context => Activator . CreateInstance ( methodInfo . DeclaringType ) ! ;
129137 }
130138
131139 var targetExpression = Expression . Convert ( TargetExpr , methodInfo . DeclaringType ) ;
132- var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , targetExpression ) ;
140+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , factoryContext , targetExpression ) ;
133141
134- return httpContext =>
135- {
136- return targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) ;
137- } ;
142+ return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) , factoryContext . Metadata ) ;
138143 }
139144
140- private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , RequestDelegateFactoryOptions ? options , Expression ? targetExpression )
145+ private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , RequestDelegateFactoryOptions ? options , FactoryContext factoryContext , Expression ? targetExpression )
141146 {
142147 // Non void return type
143148
@@ -155,11 +160,6 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
155160 // return default;
156161 // }
157162
158- var factoryContext = new FactoryContext ( )
159- {
160- ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
161- } ;
162-
163163 if ( options ? . RouteParameterNames is { } routeParameterNames )
164164 {
165165 factoryContext . RouteParameters = new ( routeParameterNames ) ;
@@ -861,6 +861,7 @@ private static Expression BindParameterFromBody(ParameterInfo parameter, bool al
861861 }
862862 }
863863
864+ factoryContext . Metadata . Add ( DefaultAcceptsMetadata ) ;
864865 var isOptional = IsOptionalParameter ( parameter ) ;
865866
866867 factoryContext . JsonRequestBodyType = parameter . ParameterType ;
@@ -1111,6 +1112,8 @@ private class FactoryContext
11111112
11121113 public Dictionary < string , string > TrackedParameters { get ; } = new ( ) ;
11131114 public bool HasMultipleBodyParameters { get ; set ; }
1115+
1116+ public List < object > Metadata { get ; } = new ( ) ;
11141117 }
11151118
11161119 private static class RequestDelegateFactoryConstants
0 commit comments