Add HubMetdata (#8425)

- Added HubMetadata to detect the hub type on Endpoints
- Added a test
This commit is contained in:
David Fowler 2019-03-12 11:33:55 -07:00 committed by GitHub
parent 21b20afb1d
commit 107e1862b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -183,6 +183,11 @@ namespace Microsoft.AspNetCore.SignalR
public abstract System.Threading.Tasks.Task SendUserAsync(string userId, string methodName, object[] args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
public abstract System.Threading.Tasks.Task SendUsersAsync(System.Collections.Generic.IReadOnlyList<string> userIds, string methodName, object[] args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
}
public partial class HubMetadata
{
public HubMetadata(System.Type hubType) { }
public System.Type HubType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
public partial class HubMethodNameAttribute : System.Attribute
{

View File

@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNetCore.SignalR
{
/// <summary>
/// Metadata that describes the <see cref="Hub"/> information associated with a specific endpoint.
/// </summary>
public class HubMetadata
{
public HubMetadata(Type hubType)
{
HubType = hubType;
}
/// <summary>
/// The type of <see cref="Hub"/>.
/// </summary>
public Type HubType { get; }
}
}

View File

@ -67,6 +67,9 @@ namespace Microsoft.AspNetCore.Builder
{
e.Metadata.Add(item);
}
// Add metadata that captures the hub type this endpoint is associated with
e.Metadata.Add(new HubMetadata(typeof(THub)));
});
return conventionBuilder;

View File

@ -185,6 +185,27 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Fact]
public void MapHubEndPointRoutingAppliesHubMetadata()
{
void ConfigureRoutes(IEndpointRouteBuilder routes)
{
// This "Foo" policy should override the default auth attribute
routes.MapHub<AuthHub>("/path");
}
using (var host = BuildWebHostWithEndPointRouting(ConfigureRoutes))
{
host.Start();
var dataSource = host.Services.GetRequiredService<EndpointDataSource>();
// We register 2 endpoints (/negotiate and /)
Assert.Equal(2, dataSource.Endpoints.Count);
Assert.Equal(typeof(AuthHub), dataSource.Endpoints[0].Metadata.GetMetadata<HubMetadata>()?.HubType);
Assert.Equal(typeof(AuthHub), dataSource.Endpoints[1].Metadata.GetMetadata<HubMetadata>()?.HubType);
}
}
private class InvalidHub : Hub
{
public void OverloadedMethod(int num)