-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Hub filters! #21278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hub filters! #21278
Changes from all commits
110c42b
21589aa
209d6f7
99ca508
6dd758e
1348d6d
5d4c356
24b67ab
c8c897e
7b07e9a
ae497dd
65a35bf
33ba19e
278a092
3493cc0
fc31337
12c3376
5c84330
463e2bb
25c4a6d
9a12a91
1d172b8
881152e
da01a19
0dc56d0
2492107
52d0fce
1e05d7c
383352d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.SignalR | ||
{ | ||
|
@@ -12,16 +14,27 @@ namespace Microsoft.AspNetCore.SignalR | |
/// </summary> | ||
public class HubInvocationContext | ||
{ | ||
internal ObjectMethodExecutor ObjectMethodExecutor { get; } | ||
|
||
/// <summary> | ||
/// Instantiates a new instance of the <see cref="HubInvocationContext"/> class. | ||
/// </summary> | ||
/// <param name="context">Context for the active Hub connection and caller.</param> | ||
/// <param name="hubType">The type of the Hub.</param> | ||
/// <param name="hubMethodName">The name of the Hub method being invoked.</param> | ||
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> specific to the scope of this Hub method invocation.</param> | ||
/// <param name="hub">The instance of the Hub.</param> | ||
/// <param name="hubMethod">The <see cref="MethodInfo"/> for the Hub method being invoked.</param> | ||
/// <param name="hubMethodArguments">The arguments provided by the client.</param> | ||
public HubInvocationContext(HubCallerContext context, Type hubType, string hubMethodName, object[] hubMethodArguments): this(context, hubMethodName, hubMethodArguments) | ||
public HubInvocationContext(HubCallerContext context, IServiceProvider serviceProvider, Hub hub, MethodInfo hubMethod, IReadOnlyList<object> hubMethodArguments) | ||
{ | ||
HubType = hubType; | ||
Hub = hub; | ||
ServiceProvider = serviceProvider; | ||
HubMethod = hubMethod; | ||
HubMethodArguments = hubMethodArguments; | ||
Context = context; | ||
|
||
#pragma warning disable CS0618 // Type or member is obsolete | ||
HubMethodName = HubMethod.Name; | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
/// <summary> | ||
|
@@ -30,11 +43,16 @@ public HubInvocationContext(HubCallerContext context, Type hubType, string hubMe | |
/// <param name="context">Context for the active Hub connection and caller.</param> | ||
/// <param name="hubMethodName">The name of the Hub method being invoked.</param> | ||
/// <param name="hubMethodArguments">The arguments provided by the client.</param> | ||
[Obsolete("This constructor is obsolete and will be removed in a future version. The recommended alternative is to use the other constructor.")] | ||
public HubInvocationContext(HubCallerContext context, string hubMethodName, object[] hubMethodArguments) | ||
{ | ||
HubMethodName = hubMethodName; | ||
HubMethodArguments = hubMethodArguments; | ||
Context = context; | ||
throw new NotSupportedException("This constructor no longer works. Use the other constructor."); | ||
} | ||
|
||
internal HubInvocationContext(ObjectMethodExecutor objectMethodExecutor, HubCallerContext context, IServiceProvider serviceProvider, Hub hub, object[] hubMethodArguments) | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: this(context, serviceProvider, hub, objectMethodExecutor.MethodInfo, hubMethodArguments) | ||
{ | ||
ObjectMethodExecutor = objectMethodExecutor; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -43,18 +61,29 @@ public HubInvocationContext(HubCallerContext context, string hubMethodName, obje | |
public HubCallerContext Context { get; } | ||
|
||
/// <summary> | ||
/// Gets the Hub type. | ||
/// Gets the Hub instance. | ||
/// </summary> | ||
public Type HubType { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we retain this property to avoid a breaking change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not breaking because this was added in 5.0 |
||
public Hub Hub { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the Hub method being invoked. | ||
/// </summary> | ||
[Obsolete("This property is obsolete and will be removed in a future version. Use HubMethod.Name instead.")] | ||
public string HubMethodName { get; } | ||
|
||
/// <summary> | ||
/// Gets the arguments provided by the client. | ||
/// </summary> | ||
public IReadOnlyList<object> HubMethodArguments { get; } | ||
|
||
/// <summary> | ||
/// The <see cref="IServiceProvider"/> specific to the scope of this Hub method invocation. | ||
/// </summary> | ||
public IServiceProvider ServiceProvider { get; } | ||
|
||
/// <summary> | ||
/// The <see cref="MethodInfo"/> for the Hub method being invoked. | ||
/// </summary> | ||
public MethodInfo HubMethod { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
pranavkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
#nullable enable | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.SignalR | ||
{ | ||
/// <summary> | ||
/// Context for the hub lifetime events <see cref="Hub.OnConnectedAsync"/> and <see cref="Hub.OnDisconnectedAsync(Exception)"/>. | ||
/// </summary> | ||
public sealed class HubLifetimeContext | ||
{ | ||
/// <summary> | ||
/// Instantiates a new instance of the <see cref="HubLifetimeContext"/> class. | ||
/// </summary> | ||
/// <param name="context">Context for the active Hub connection and caller.</param> | ||
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> specific to the scope of this Hub method invocation.</param> | ||
/// <param name="hub">The instance of the Hub.</param> | ||
public HubLifetimeContext(HubCallerContext context, IServiceProvider serviceProvider, Hub hub) | ||
{ | ||
Hub = hub; | ||
ServiceProvider = serviceProvider; | ||
Context = context; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the context for the active Hub connection and caller. | ||
/// </summary> | ||
public HubCallerContext Context { get; } | ||
|
||
/// <summary> | ||
/// Gets the Hub instance. | ||
/// </summary> | ||
public Hub Hub { get; } | ||
|
||
/// <summary> | ||
/// The <see cref="IServiceProvider"/> specific to the scope of this Hub method invocation. | ||
/// </summary> | ||
public IServiceProvider ServiceProvider { get; } | ||
} | ||
} | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,60 @@ | ||||||
// 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. | ||||||
|
||||||
#nullable enable | ||||||
|
||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using Microsoft.AspNetCore.SignalR.Internal; | ||||||
|
||||||
namespace Microsoft.AspNetCore.SignalR | ||||||
{ | ||||||
/// <summary> | ||||||
/// Methods to add <see cref="IHubFilter"/>'s to Hubs. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// </summary> | ||||||
public static class HubOptionsExtensions | ||||||
{ | ||||||
/// <summary> | ||||||
/// Adds an instance of an <see cref="IHubFilter"/> to the <see cref="HubOptions"/>. | ||||||
/// </summary> | ||||||
/// <param name="options">The options to add a filter to.</param> | ||||||
/// <param name="hubFilter">The filter instance to add to the options.</param> | ||||||
public static void AddFilter(this HubOptions options, IHubFilter hubFilter) | ||||||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
_ = options ?? throw new ArgumentNullException(nameof(options)); | ||||||
_ = hubFilter ?? throw new ArgumentNullException(nameof(hubFilter)); | ||||||
|
||||||
if (options.HubFilters == null) | ||||||
{ | ||||||
options.HubFilters = new List<IHubFilter>(); | ||||||
} | ||||||
|
||||||
options.HubFilters.Add(hubFilter); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Adds an <see cref="IHubFilter"/> type to the <see cref="HubOptions"/> that will be resolved via DI or type activated. | ||||||
/// </summary> | ||||||
/// <typeparam name="TFilter">The <see cref="IHubFilter"/> type that will be added to the options.</typeparam> | ||||||
/// <param name="options">The options to add a filter to.</param> | ||||||
public static void AddFilter<TFilter>(this HubOptions options) where TFilter : IHubFilter | ||||||
{ | ||||||
_ = options ?? throw new ArgumentNullException(nameof(options)); | ||||||
|
||||||
options.AddFilter(typeof(TFilter)); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Adds an <see cref="IHubFilter"/> type to the <see cref="HubOptions"/> that will be resolved via DI or type activated. | ||||||
/// </summary> | ||||||
/// <param name="options">The options to add a filter to.</param> | ||||||
/// <param name="filterType">The <see cref="IHubFilter"/> type that will be added to the options.</param> | ||||||
public static void AddFilter(this HubOptions options, Type filterType) | ||||||
{ | ||||||
_ = options ?? throw new ArgumentNullException(nameof(options)); | ||||||
_ = filterType ?? throw new ArgumentNullException(nameof(filterType)); | ||||||
|
||||||
options.AddFilter(new HubFilterFactory(filterType)); | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we decided in API review that we were not going to obsolete HubMethodName.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe. No one commented that so it was forgotten. Fortunately we can easily fix this :)