4
4
using System . Diagnostics ;
5
5
using System . Linq ;
6
6
using System . Linq . Expressions ;
7
+ using System . Net . Http ;
7
8
using System . Reflection ;
8
9
using System . Security . Claims ;
9
10
using System . Text ;
@@ -63,14 +64,16 @@ public static partial class RequestDelegateFactory
63
64
private static readonly BinaryExpression TempSourceStringNotNullExpr = Expression . NotEqual ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
64
65
private static readonly BinaryExpression TempSourceStringNullExpr = Expression . Equal ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
65
66
67
+ private static readonly AcceptsMetadata DefaultAcceptsMetadata = new ( new [ ] { "application/json" } ) ;
68
+
66
69
/// <summary>
67
70
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="action"/>.
68
71
/// </summary>
69
72
/// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
70
73
/// <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>
72
75
#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 )
74
77
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
75
78
{
76
79
if ( action is null )
@@ -84,12 +87,15 @@ public static RequestDelegate Create(Delegate action, RequestDelegateFactoryOpti
84
87
null => null ,
85
88
} ;
86
89
87
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , options , targetExpression ) ;
88
-
89
- return httpContext =>
90
+ var factoryContext = new FactoryContext
90
91
{
91
- return targetableRequestDelegate ( action . Target , httpContext ) ;
92
+ ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
92
93
} ;
94
+
95
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , options , factoryContext , targetExpression ) ;
96
+
97
+ return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( action . Target , httpContext ) , factoryContext . Metadata ) ;
98
+
93
99
}
94
100
95
101
/// <summary>
@@ -100,7 +106,7 @@ public static RequestDelegate Create(Delegate action, RequestDelegateFactoryOpti
100
106
/// <param name="options">The <see cref="RequestDelegateFactoryOptions"/> used to configure the behavior of the handler.</param>
101
107
/// <returns>The <see cref="RequestDelegate"/>.</returns>
102
108
#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 )
104
110
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
105
111
{
106
112
if ( methodInfo is null )
@@ -113,31 +119,30 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
113
119
throw new ArgumentException ( $ "{ nameof ( methodInfo ) } does not have a declaring type.") ;
114
120
}
115
121
122
+ var factoryContext = new FactoryContext
123
+ {
124
+ ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
125
+ } ;
126
+
116
127
if ( targetFactory is null )
117
128
{
118
129
if ( methodInfo . IsStatic )
119
130
{
120
- var untargetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , targetExpression : null ) ;
131
+ var untargetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , factoryContext , targetExpression : null ) ;
121
132
122
- return httpContext =>
123
- {
124
- return untargetableRequestDelegate ( null , httpContext ) ;
125
- } ;
133
+ return new RequestDelegateResult ( httpContext => untargetableRequestDelegate ( null , httpContext ) , factoryContext . Metadata ) ;
126
134
}
127
135
128
136
targetFactory = context => Activator . CreateInstance ( methodInfo . DeclaringType ) ! ;
129
137
}
130
138
131
139
var targetExpression = Expression . Convert ( TargetExpr , methodInfo . DeclaringType ) ;
132
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , targetExpression ) ;
140
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , options , factoryContext , targetExpression ) ;
133
141
134
- return httpContext =>
135
- {
136
- return targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) ;
137
- } ;
142
+ return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) , factoryContext . Metadata ) ;
138
143
}
139
144
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 )
141
146
{
142
147
// Non void return type
143
148
@@ -155,11 +160,6 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
155
160
// return default;
156
161
// }
157
162
158
- var factoryContext = new FactoryContext ( )
159
- {
160
- ServiceProviderIsService = options ? . ServiceProvider ? . GetService < IServiceProviderIsService > ( )
161
- } ;
162
-
163
163
if ( options ? . RouteParameterNames is { } routeParameterNames )
164
164
{
165
165
factoryContext . RouteParameters = new ( routeParameterNames ) ;
@@ -861,6 +861,7 @@ private static Expression BindParameterFromBody(ParameterInfo parameter, bool al
861
861
}
862
862
}
863
863
864
+ factoryContext . Metadata . Add ( DefaultAcceptsMetadata ) ;
864
865
var isOptional = IsOptionalParameter ( parameter ) ;
865
866
866
867
factoryContext . JsonRequestBodyType = parameter . ParameterType ;
@@ -1111,6 +1112,8 @@ private class FactoryContext
1111
1112
1112
1113
public Dictionary < string , string > TrackedParameters { get ; } = new ( ) ;
1113
1114
public bool HasMultipleBodyParameters { get ; set ; }
1115
+
1116
+ public List < object > Metadata { get ; } = new ( ) ;
1114
1117
}
1115
1118
1116
1119
private static class RequestDelegateFactoryConstants
0 commit comments