Skip to content

Commit d9c6ca6

Browse files
authored
Fix awaitable response type comparison in async pipelines (#43)
* Add test for request handler with async enumerable response * Fix awaitable response type comparison in async pipelines
1 parent ed65b23 commit d9c6ca6

6 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/DispatchR/Configuration/ServiceRegistrator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public static void RegisterHandlers(IServiceCollection services, List<Type> allT
125125
continue;
126126
}
127127

128+
// If both are non-generic (Task vs ValueTask), then compare directly
129+
if (!responseTypeArg.GenericTypeArguments.Any() &&
130+
!genericHandlerResponseType.GenericTypeArguments.Any() &&
131+
responseTypeArg != genericHandlerResponseType)
132+
{
133+
continue; // Task != ValueTask, so skip
134+
}
135+
128136
// register async generic pipelines
129137
if (responseTypeArg.GenericTypeArguments.Any())
130138
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace DispatchR.TestCommon.Fixtures.Interfaces;
2+
3+
public interface INonGenericInterface;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.CompilerServices;
2+
using DispatchR.Abstractions.Send;
3+
4+
namespace DispatchR.TestCommon.Fixtures.SendRequest.AsyncEnumerable;
5+
6+
public class AsyncEnumerableHandler : IRequestHandler<AsyncEnumerableRequest, IAsyncEnumerable<int>>
7+
{
8+
public async IAsyncEnumerable<int> Handle(AsyncEnumerableRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
9+
{
10+
yield return await System.Threading.Tasks.Task.FromResult(1);
11+
yield return await System.Threading.Tasks.Task.FromResult(2);
12+
yield return await System.Threading.Tasks.Task.FromResult(3);
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using DispatchR.Abstractions.Send;
2+
3+
namespace DispatchR.TestCommon.Fixtures.SendRequest.AsyncEnumerable;
4+
5+
public class AsyncEnumerableRequest : IRequest<AsyncEnumerableRequest, IAsyncEnumerable<int>>;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.CompilerServices;
2+
using DispatchR.Abstractions.Send;
3+
using DispatchR.TestCommon.Fixtures.Interfaces;
4+
5+
namespace DispatchR.TestCommon.Fixtures.SendRequest;
6+
7+
public class AsyncEnumerablePipelineBehavior<TRequest, TResponse>
8+
: INonGenericInterface,
9+
IPipelineBehavior<TRequest, IAsyncEnumerable<TResponse>>
10+
where TRequest : class, IRequest<TRequest, IAsyncEnumerable<TResponse>>, new()
11+
{
12+
public required IRequestHandler<TRequest, IAsyncEnumerable<TResponse>> NextPipeline { get; set; }
13+
14+
public async IAsyncEnumerable<TResponse> Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
15+
{
16+
await foreach (var item in NextPipeline.Handle(request, cancellationToken))
17+
{
18+
yield return item;
19+
}
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using DispatchR.Abstractions.Send;
2+
3+
namespace DispatchR.TestCommon.Fixtures.SendRequest;
4+
5+
public class GenericPipelineBehaviorTaskWithoutResponse<TRequest, TResponse>()
6+
: IPipelineBehavior<TRequest, System.Threading.Tasks.Task>
7+
where TRequest : class, IRequest<TRequest, System.Threading.Tasks.Task>, new()
8+
{
9+
public System.Threading.Tasks.Task Handle(TRequest request, CancellationToken cancellationToken)
10+
{
11+
return NextPipeline.Handle(request, cancellationToken);
12+
}
13+
14+
public required IRequestHandler<TRequest, System.Threading.Tasks.Task> NextPipeline { get; set; }
15+
}

0 commit comments

Comments
 (0)