SignalR with message pack does not work with union interfaces
To reproduce the issue, first configure signalR to use message pack:
services.AddSignalR().AddMessagePackProtocol()
In your hub, introduce a method that takes an interface as an argument:
public Task ExecuteAction(IAction action)
In the interface, specify a union attribute that points to a concrete implementation:
[Union(0, typeof(ActionImpl))] //ActionImpl implements the interface
public interface IAction
From the client, pass the concrete implementation as an argument to the method:
var hubConnection = new HubConnectionBuilder().WithUrl("http://a/hub").AddMessagePackProtocol().Build();
await hubConnection.StartAsync();
await hubConnection.InvokeAsync("ExecuteAction", new ActionImpl());
The invocation results in the below exception:
Microsoft.AspNetCore.SignalR.HubException: Failed to invoke 'ExecuteAction' due to an error on the server. InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsync(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
Changing the method parameter to be the concrete implementation works.
Assemblies & Versions: Server: Microsoft.AspNetCore.App: 2.1.1 Microsoft.Azure.SignalR: 1.0.0 Microsoft.AspNetCore.SignalR.Protocols.MessagePack: 1.1.0-preview2-35157
Client: Microsoft.AspNetCore.SignalR.Client: 1.1.0-preview2-35157 Microsoft.AspNetCore.SignalR.Client.Core: 1.1.0-preview2-35157 Microsoft.AspNetCore.SignalR.Protocols.MessagePack: 1.1.0-preview2-35157
Does it work if the union attribute is used on a field rather than on the parameter itself? We do some special stuff with the parameters but the fields/properties on those types should work fine.
You are right, it works as expected when using the interface (with the union attribute) as a field rather than as a method parameter directly.
Ok, I'll move this bug in to 3.0 and we can consider what we can do to improve this in the future. We'll have to investigate further what we can do to support these attributes. If it's just a matter of calling the right methods in MessagePack-CSharp, we can do that, but if it's more complicated, we may need to revisit it.
In the meantime, the workaround of putting the interface in a field or property should work.
I don't think we can resolve this issue in SignalR since the serializer doesn't actually understand the parameter. The attributes would need to be on the parameter and we'd need to provide the attributes as part of serialization. Neither MessagePack-CSharp nor Json.NET really support this.