dotnet / dotnet/aspnetcore

Allow to check the maximum size of the SignalR message on the client side

Open
#54,068 0 comments 0 reactions 0 assignees View on GitHub
area-signalr
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I am trying to handle in the client that if the message to send exceeds the maximum message size allowed in the server I can show at least a log so that if the connection fails at least I can know the cause.

I know that using on the server side `EnableDetailedErrors = true` could achieve the same but that property is very generic and if any other error occurs I don't want it to expose information that could be sensitive.

That is why I am doing some reverse engineering and although it is not pretty, I think there is no other alternative to do this as of today:

```csharp
static int InvocationMessageByteCount(HubConnection hubConnection, string invocationId, string target, object[] arguments)
{
IHubProtocol hubProtocol = (IHubProtocol)hubConnection.GetType()
.GetField("_protocol", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(hubConnection);

InvocationMessage invocationMessage = new(invocationId, target, arguments);

ArrayBufferWriter data = new();

hubProtocol.WriteMessage(invocationMessage, data);

return data.WrittenCount;
}
```

And to know if it exceeds the message size limit is simply to make a check of that value: "byteCount > 32768" (default value of the server).

### Describe the solution you'd like

That there is an API similar to the method I have made included by default.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.