Better exception for Generic methods on Hub (#15388)

This commit is contained in:
Brennan 2019-10-29 16:02:39 -07:00 committed by GitHub
parent 814a37548b
commit 0d02d7a705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -547,6 +547,11 @@ namespace Microsoft.AspNetCore.SignalR.Internal
foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType))
{
if (methodInfo.IsGenericMethod)
{
throw new NotSupportedException($"Method '{methodInfo.Name}' is a generic method which is not supported on a Hub.");
}
var methodName =
methodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
methodInfo.Name;

View File

@ -569,6 +569,13 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
public class GenericMethodHub : Hub
{
public void GenericMethod<T>()
{
}
}
public class DisposeTrackingHub : TestHub
{
private readonly TrackDispose _trackDispose;

View File

@ -1327,6 +1327,19 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Fact]
public void CannotHaveGenericMethodOnHub()
{
using (StartVerifiableLog())
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(null, LoggerFactory);
var exception = Assert.Throws<NotSupportedException>(() => serviceProvider.GetService<HubConnectionHandler<GenericMethodHub>>());
Assert.Equal("Method 'GenericMethod' is a generic method which is not supported on a Hub.", exception.Message);
}
}
[Theory]
[MemberData(nameof(HubTypes))]
public async Task BroadcastHubMethodSendsToAllClients(Type hubType)