dotnet / dotnet/aspnetcore

Consider allowing hinheriting HubMethodNameAttribute in SignalR hub classes

Open
#64,291 1 comment 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.

When using a strongly typed SignalR hub, `HubMethodNameAttribute` in the client type is used, but the same doesn't happen if the server methods are declared in an implemented interface.

Given:

```csharp
public interface INotifications
{
public const string ReceiveMessageMethodName = "ReceiveMessage";

[HubMethodName(ReceiveMessageMethodName)]
public Task ReceiveMessageAsync(string user, string message);
}
public interface IMethods
{
public const string SendMessageMethodName = "SendMessage";

[HubMethodName(SendMessageMethodName)]
public Task SendMessageAsync(string user, string message);
}
```

In this hub:

```csharp
public class NotificationsHub : Hub, IMethods
{
public async Task SendMessageAsync(string user, string message)
{
await Clients.All.ReceiveMessageAsync(user, message);
}
}
```

The client method is `ReceiveMessage` and the server method is `SendMessageAsync`.

### Describe the solution you'd like

The server method should be `SendMessage`.

### Additional context

By changing [this code](https://github.com/dotnet/aspnetcore/blob/25e67e3519c246b4c273430bdee0c177317847b9/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs#L785-L787) to:

```csharp
var methodName =
methodInfo.GetCustomAttribute(inherit: true)?.Name ??
methodInfo.Name;
```

it should work.

This is a breaking change, though,

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.