Skip to content

Commit 32898d2

Browse files
Support struct IResults (#37344)
1 parent 34ed6d4 commit 32898d2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
487487
}
488488
else if (typeof(IResult).IsAssignableFrom(returnType))
489489
{
490+
if (returnType.IsValueType)
491+
{
492+
var box = Expression.TypeAs(methodCall, typeof(IResult));
493+
return Expression.Call(ResultWriteResponseAsyncMethod, box, HttpContextExpr);
494+
}
490495
return Expression.Call(ResultWriteResponseAsyncMethod, methodCall, HttpContextExpr);
491496
}
492497
else if (returnType == typeof(string))

src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,10 @@ public static IEnumerable<object[]> CustomResults
20032003
static Task<object> StaticTaskOfIResultAsObject() => Task.FromResult<object>(new CustomResult("Still not enough tests!"));
20042004
static ValueTask<object> StaticValueTaskOfIResultAsObject() => ValueTask.FromResult<object>(new CustomResult("Still not enough tests!"));
20052005

2006+
StructResult TestStructAction() => new StructResult(resultString);
2007+
Task<StructResult> TaskTestStructAction() => Task.FromResult(new StructResult(resultString));
2008+
ValueTask<StructResult> ValueTaskTestStructAction() => ValueTask.FromResult(new StructResult(resultString));
2009+
20062010
return new List<object[]>
20072011
{
20082012
new object[] { (Func<CustomResult>)TestAction },
@@ -2021,6 +2025,10 @@ public static IEnumerable<object[]> CustomResults
20212025

20222026
new object[] { (Func<Task<object>>)StaticTaskOfIResultAsObject},
20232027
new object[] { (Func<ValueTask<object>>)StaticValueTaskOfIResultAsObject},
2028+
2029+
new object[] { (Func<StructResult>)TestStructAction },
2030+
new object[] { (Func<Task<StructResult>>)TaskTestStructAction },
2031+
new object[] { (Func<ValueTask<StructResult>>)ValueTaskTestStructAction },
20242032
};
20252033
}
20262034
}
@@ -3247,6 +3255,21 @@ public Task ExecuteAsync(HttpContext httpContext)
32473255
}
32483256
}
32493257

3258+
private struct StructResult : IResult
3259+
{
3260+
private readonly string _resultString;
3261+
3262+
public StructResult(string resultString)
3263+
{
3264+
_resultString = resultString;
3265+
}
3266+
3267+
public Task ExecuteAsync(HttpContext httpContext)
3268+
{
3269+
return httpContext.Response.WriteAsync(_resultString);
3270+
}
3271+
}
3272+
32503273
private class ExceptionThrowingRequestBodyStream : Stream
32513274
{
32523275
private readonly Exception _exceptionToThrow;

0 commit comments

Comments
 (0)