Skip to content

Commit 20b0a2c

Browse files
committed
progress
1 parent 57b2bcd commit 20b0a2c

20 files changed

+165
-154
lines changed

src/Components/Server/src/BlazorPack/BlazorPackHubProtocol.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.AspNetCore.Components.Server.BlazorPack
1919
/// <summary>
2020
/// Implements the SignalR Hub Protocol using MessagePack with limited type support.
2121
/// </summary>
22+
[NonDefaultHubProtocol]
2223
internal sealed class BlazorPackHubProtocol : IHubProtocol
2324
{
2425
internal const string ProtocolName = "blazorpack";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNetCore.Components.Server.BlazorPack
7+
{
8+
// Tells SignalR not to add the IHubProtocol with this attribute to all hubs by default
9+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
10+
internal class NonDefaultHubProtocol : Attribute
11+
{
12+
}
13+
}

src/Components/Server/src/DependencyInjection/ComponentServiceCollectionExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public static IServerSideBlazorBuilder AddServerSideBlazor(this IServiceCollecti
4545
services.AddSignalR().AddHubOptions<ComponentHub>(options =>
4646
{
4747
options.SupportedProtocols.Clear();
48-
49-
// Add Blazor specific hub protocol here instead of in DI
50-
// Because adding to DI would add this protocol to all Hubs by default
51-
options.AdditionalHubProtocols.Add(new BlazorPackHubProtocol());
48+
options.SupportedProtocols.Add(BlazorPackHubProtocol.ProtocolName);
5249
});
5350

51+
// Register the Blazor specific hub protocol
52+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHubProtocol, BlazorPackHubProtocol>());
53+
5454
// Here we add a bunch of services that don't vary in any way based on the
5555
// user's configuration. So even if the user has multiple independent server-side
5656
// Components entrypoints, this lot is the same and repeated registrations are a no-op.

src/Components/Server/test/DependencyInjection/ComponentServiceCollectionExtensionsTest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ public void AddServerSideSignalR_RegistersBlazorPack()
2323
var options = services.BuildServiceProvider().GetRequiredService<IOptions<HubOptions<ComponentHub>>>();
2424

2525
// Assert
26-
Assert.Empty(options.Value.SupportedProtocols);
27-
var protocol = Assert.Single(options.Value.AdditionalHubProtocols);
28-
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol.Name);
26+
var protocol = Assert.Single(options.Value.SupportedProtocols);
27+
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol);
2928
}
3029

3130
[Fact]
@@ -45,9 +44,8 @@ public void AddServerSideSignalR_RespectsGlobalHubOptions()
4544
var options = services.BuildServiceProvider().GetRequiredService<IOptions<HubOptions<ComponentHub>>>();
4645

4746
// Assert
48-
Assert.Empty(options.Value.SupportedProtocols);
49-
var protocol = Assert.Single(options.Value.AdditionalHubProtocols);
50-
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol.Name);
47+
var protocol = Assert.Single(options.Value.SupportedProtocols);
48+
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol);
5149
Assert.Equal(TimeSpan.FromMinutes(10), options.Value.HandshakeTimeout);
5250
}
5351

@@ -73,9 +71,8 @@ public void AddServerSideSignalR_ConfiguresGlobalOptionsBeforePerHubOptions()
7371
var globalOptions = services.BuildServiceProvider().GetRequiredService<IOptions<HubOptions>>();
7472

7573
// Assert
76-
Assert.Empty(options.Value.SupportedProtocols);
77-
var protocol = Assert.Single(options.Value.AdditionalHubProtocols);
78-
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol.Name);
74+
var protocol = Assert.Single(options.Value.SupportedProtocols);
75+
Assert.Equal(BlazorPackHubProtocol.ProtocolName, protocol);
7976
Assert.Equal(TimeSpan.FromMinutes(5), options.Value.HandshakeTimeout);
8077

8178
// Configuring Blazor options is kept separate from the global options.

src/SignalR/perf/Microbenchmarks/HubConnectionContextBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task SuccessHandshakeAsync()
6161
_pipe.AddReadResult(new ValueTask<ReadResult>(_handshakeRequestResult));
6262

6363
await _hubConnectionContext.HandshakeAsync(TimeSpan.FromSeconds(5), _supportedProtocols, _successHubProtocolResolver,
64-
_userIdProvider, enableDetailedErrors: true, Array.Empty<IHubProtocol>());
64+
_userIdProvider, enableDetailedErrors: true);
6565
}
6666

6767
[Benchmark]
@@ -70,7 +70,7 @@ public async Task ErrorHandshakeAsync()
7070
_pipe.AddReadResult(new ValueTask<ReadResult>(_handshakeRequestResult));
7171

7272
await _hubConnectionContext.HandshakeAsync(TimeSpan.FromSeconds(5), _supportedProtocols, _failureHubProtocolResolver,
73-
_userIdProvider, enableDetailedErrors: true, Array.Empty<IHubProtocol>());
73+
_userIdProvider, enableDetailedErrors: true);
7474
}
7575
}
7676

src/SignalR/perf/Microbenchmarks/RedisProtocolBenchmark.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
1313
{
1414
public class RedisProtocolBenchmark
1515
{
16-
private RedisProtocol<TestHub> _protocol;
16+
private RedisProtocol<Hub> _protocol;
1717
private RedisGroupCommand _groupCommand;
1818
private object[] _args;
1919
private string _methodName;
@@ -28,7 +28,7 @@ public class RedisProtocolBenchmark
2828
[GlobalSetup]
2929
public void GlobalSetup()
3030
{
31-
_protocol = new RedisProtocol<TestHub>(new [] {
31+
_protocol = new RedisProtocol<Hub>(new [] {
3232
new DummyProtocol("protocol1"),
3333
new DummyProtocol("protocol2")
3434
});
@@ -152,8 +152,5 @@ public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
152152
return HubProtocolExtensions.GetMessageBytes(this, message);
153153
}
154154
}
155-
156-
private class TestHub : Hub
157-
{ }
158155
}
159156
}

src/SignalR/server/Core/src/HubConnectionContext.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private void AbortAllowReconnect()
385385
}
386386

387387
internal async Task<bool> HandshakeAsync(TimeSpan timeout, IReadOnlyList<string> supportedProtocols, IHubProtocolResolver protocolResolver,
388-
IUserIdProvider userIdProvider, bool enableDetailedErrors, IList<IHubProtocol> additionalProtocols)
388+
IUserIdProvider userIdProvider, bool enableDetailedErrors)
389389
{
390390
try
391391
{
@@ -437,15 +437,6 @@ internal async Task<bool> HandshakeAsync(TimeSpan timeout, IReadOnlyList<string>
437437
Protocol = protocolResolver.GetProtocol(handshakeRequestMessage.Protocol, supportedProtocols);
438438
if (Protocol == null)
439439
{
440-
foreach (var protocol in additionalProtocols)
441-
{
442-
if (string.Equals(handshakeRequestMessage.Protocol, protocol.Name, StringComparison.OrdinalIgnoreCase))
443-
{
444-
Protocol = protocol;
445-
break;
446-
}
447-
}
448-
449440
if (Protocol == null)
450441
{
451442
Log.HandshakeFailed(_logger, null);

src/SignalR/server/Core/src/HubConnectionHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
7777
// Then set the keepAlive and handshakeTimeout values to the defaults in HubOptionsSetup when they were explicitly set to null.
7878

7979
var supportedProtocols = _hubOptions.SupportedProtocols ?? _globalHubOptions.SupportedProtocols;
80-
var additionalProtocols = _hubOptions.AdditionalHubProtocols ?? _globalHubOptions.AdditionalHubProtocols ?? Array.Empty<IHubProtocol>();
81-
if ((supportedProtocols == null || supportedProtocols.Count == 0) && additionalProtocols.Count == 0)
80+
if (supportedProtocols == null || supportedProtocols.Count == 0)
8281
{
8382
throw new InvalidOperationException("There are no supported protocols");
8483
}
@@ -98,7 +97,7 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
9897
var connectionContext = new HubConnectionContext(connection, contextOptions, _loggerFactory);
9998

10099
var resolvedSupportedProtocols = (supportedProtocols as IReadOnlyList<string>) ?? supportedProtocols.ToList();
101-
if (!await connectionContext.HandshakeAsync(handshakeTimeout, resolvedSupportedProtocols, _protocolResolver, _userIdProvider, _enableDetailedErrors, additionalProtocols))
100+
if (!await connectionContext.HandshakeAsync(handshakeTimeout, resolvedSupportedProtocols, _protocolResolver, _userIdProvider, _enableDetailedErrors))
102101
{
103102
return;
104103
}

src/SignalR/server/Core/src/HubOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,5 @@ public class HubOptions
5252
/// Gets or sets the max buffer size for client upload streams. The default size is 10.
5353
/// </summary>
5454
public int? StreamBufferCapacity { get; set; } = null;
55-
56-
/// <summary>
57-
/// Add protocols specific to this Hub so other Hubs do not get these protocols by default.
58-
/// When using this you do not need to add the IHubProtocol to DI.
59-
/// </summary>
60-
public IList<IHubProtocol> AdditionalHubProtocols { get; set; } = null;
6155
}
6256
}

src/SignalR/server/Core/src/HubOptionsSetup.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using Microsoft.AspNetCore.SignalR.Protocol;
78
using Microsoft.Extensions.Options;
89

@@ -26,6 +27,10 @@ public HubOptionsSetup(IEnumerable<IHubProtocol> protocols)
2627
{
2728
foreach (var hubProtocol in protocols)
2829
{
30+
if (hubProtocol.GetType().CustomAttributes.Where(a => a.AttributeType.Name == "NonDefaultHubProtocol").Any())
31+
{
32+
continue;
33+
}
2934
_defaultProtocols.Add(hubProtocol.Name);
3035
}
3136
}
@@ -54,11 +59,6 @@ public void Configure(HubOptions options)
5459
options.SupportedProtocols = new List<string>();
5560
}
5661

57-
if (options.AdditionalHubProtocols == null)
58-
{
59-
options.AdditionalHubProtocols = new List<IHubProtocol>();
60-
}
61-
6262
if (options.StreamBufferCapacity == null)
6363
{
6464
options.StreamBufferCapacity = DefaultStreamBufferCapacity;

src/SignalR/server/Core/src/HubOptionsSetup`T.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using Microsoft.AspNetCore.SignalR.Protocol;
65
using Microsoft.Extensions.Options;
76

87
namespace Microsoft.AspNetCore.SignalR
@@ -25,13 +24,6 @@ public void Configure(HubOptions<THub> options)
2524
}
2625
options.KeepAliveInterval = _hubOptions.KeepAliveInterval;
2726
options.HandshakeTimeout = _hubOptions.HandshakeTimeout;
28-
29-
// Do a deep copy, otherwise users modifying the HubOptions<THub> list would be changing the global options list
30-
options.AdditionalHubProtocols = new List<IHubProtocol>(_hubOptions.AdditionalHubProtocols.Count);
31-
foreach (var protocol in _hubOptions.AdditionalHubProtocols)
32-
{
33-
options.AdditionalHubProtocols.Add(protocol);
34-
}
3527
}
3628
}
3729
}

src/SignalR/server/Core/src/IHubMessageSerializer`T.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/SignalR/server/Core/src/SerializedHubMessage.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,30 @@ public ReadOnlyMemory<byte> GetSerializedMessage(IHubProtocol protocol)
7474
/// Gets all serialized hub messages for all protocols used on this <see cref="SerializedHubMessage"/> instance.
7575
/// </summary>
7676
/// <returns>An <see cref="IEnumerable{T}"/> of already serialized hub messages for each protocol.</returns>
77-
public IEnumerable<SerializedMessage> GetAllSerializations()
77+
public IReadOnlyList<SerializedMessage> GetAllSerializations()
7878
{
7979
// Even if this is only used in tests, let's do it right.
8080
lock (_lock)
8181
{
8282
if (_cachedItem1.ProtocolName == null)
8383
{
84-
yield break;
84+
return Array.Empty<SerializedMessage>();
8585
}
8686

87-
yield return _cachedItem1;
87+
var list = new List<SerializedMessage>(2);
88+
list.Add(_cachedItem1);
8889

8990
if (_cachedItem2.ProtocolName != null)
9091
{
91-
yield return _cachedItem2;
92+
list.Add(_cachedItem2);
9293

9394
if (_cachedItems != null)
9495
{
95-
foreach (var item in _cachedItems)
96-
{
97-
yield return item;
98-
}
96+
list.AddRange(_cachedItems);
9997
}
10098
}
99+
100+
return list;
101101
}
102102
}
103103

src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection servi
2323
services.TryAddSingleton<SignalRCoreMarkerService>();
2424
services.TryAddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
2525
services.TryAddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
26-
services.TryAddSingleton(typeof(IHubMessageSerializer<>), typeof(DefaultHubMessageSerializer<>));
2726
services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
2827
services.TryAddSingleton(typeof(IHubContext<,>), typeof(HubContext<,>));
2928
services.TryAddSingleton(typeof(HubConnectionHandler<>), typeof(HubConnectionHandler<>));

0 commit comments

Comments
 (0)