fix(dispatcher): throw on ambiguous handlers instead of silent .First()
Multiple handlers for the same request were previously resolved via .First(), with ordering depending on Type.GetInterfaces() order (non-deterministic). Now ScalarRequestInvoker/StreamRequestInvoker throw InvalidOperationException listing the candidate handlers when more than one matches.
This commit is contained in:
parent
f7263c33a8
commit
f79e8780f7
7 changed files with 134 additions and 9 deletions
|
|
@ -43,7 +43,20 @@ internal sealed class ScalarRequestInvoker<TRequest, TResponse> : ScalarRequestI
|
|||
|
||||
Task<TResponse> Head(IScalarRequest<TResponse> r, CancellationToken ct)
|
||||
{
|
||||
return options.GetRequestHandlers<IScalarRequestHandler<TRequest, TResponse>>(serviceProvider).First().HandleAsync((TRequest)r, ct);
|
||||
var handlers = options.GetRequestHandlers<IScalarRequestHandler<TRequest, TResponse>>(serviceProvider).ToArray();
|
||||
|
||||
if (handlers.Length is 0)
|
||||
{
|
||||
throw new InvalidOperationException($"No handler is registered for request '{typeof(TRequest).FullName}'.");
|
||||
}
|
||||
|
||||
if (handlers.Length > 1)
|
||||
{
|
||||
var candidates = string.Join(", ", handlers.Select(handler => handler.GetType().FullName));
|
||||
throw new InvalidOperationException($"Multiple handlers are registered for request '{typeof(TRequest).FullName}'. Ambiguous handlers: {candidates}");
|
||||
}
|
||||
|
||||
return handlers[0].HandleAsync((TRequest)r, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue