Skip to content

IInvocationBinder throws or returns empty array inconsistently #63084

@AArnott

Description

@AArnott

In writing my own IHubProtocol implementation, I found that when a method is invoked, there is no good way to determine whether a method exists with the target name to invoke. As a result, I get unhelpful exceptions thrown.

In particular, while deserializing the invocation request I need the parameter types. I call IInvocationBinder.GetParameterTypes passing in the name of the target method that I have already deserialized. When the method does not exist, it simply returns an empty array, causing my BindArguments method to throw because the number of arguments and the number of parameters mismatch. But that's a red herring. The real issue is that the method itself does not exist at all.

I notice that IInvocationBinder.GetReturnType does throw when the target method does not exist.

Why the two diverging and undocumented policies for these two methods?

What is the best way to handle this?

Type IInvocationBinder.GetReturnType(string invocationId)
{
if (!TryGetInvocation(invocationId, out var irq))
{
Log.ReceivedUnexpectedResponse(_logger, invocationId);
throw new KeyNotFoundException($"No invocation with id '{invocationId}' could be found.");
}
return irq.ResultType;
}
Type IInvocationBinder.GetStreamItemType(string invocationId)
{
// previously, streaming was only server->client, and used GetReturnType for StreamItems
// literally the same code as the above method
if (!TryGetInvocation(invocationId, out var irq))
{
Log.ReceivedUnexpectedResponse(_logger, invocationId);
throw new KeyNotFoundException($"No invocation with id '{invocationId}' could be found.");
}
return irq.ResultType;
}
IReadOnlyList<Type> IInvocationBinder.GetParameterTypes(string methodName)
{
if (!_hubConnection._handlers.TryGetValue(methodName, out var invocationHandlerList))
{
Log.MissingHandler(_logger, methodName);
return Type.EmptyTypes;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions