Skip to content

Commit 30fe3a2

Browse files
authored
Adopt Bedrock client abstractions in SignalR client (#11484)
1 parent 82149f9 commit 30fe3a2

File tree

41 files changed

+527
-297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+527
-297
lines changed

src/Servers/Connections.Abstractions/ref/Microsoft.AspNetCore.Connections.Abstractions.netstandard2.0.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public partial interface IConnectionBuilder
109109
Microsoft.AspNetCore.Connections.ConnectionDelegate Build();
110110
Microsoft.AspNetCore.Connections.IConnectionBuilder Use(System.Func<Microsoft.AspNetCore.Connections.ConnectionDelegate, Microsoft.AspNetCore.Connections.ConnectionDelegate> middleware);
111111
}
112+
public partial interface IConnectionFactory
113+
{
114+
System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Connections.ConnectionContext> ConnectAsync(System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
115+
}
112116
public partial interface IConnectionListener : System.IAsyncDisposable
113117
{
114118
System.Net.EndPoint EndPoint { get; }
@@ -125,6 +129,11 @@ public enum TransferFormat
125129
Binary = 1,
126130
Text = 2,
127131
}
132+
public partial class UriEndPoint : System.Net.EndPoint
133+
{
134+
public UriEndPoint(System.Uri uri) { }
135+
public System.Uri Uri { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
136+
}
128137
}
129138
namespace Microsoft.AspNetCore.Connections.Features
130139
{

src/Servers/Connections.Abstractions/ref/Microsoft.AspNetCore.Connections.Abstractions.netstandard2.1.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public partial interface IConnectionBuilder
109109
Microsoft.AspNetCore.Connections.ConnectionDelegate Build();
110110
Microsoft.AspNetCore.Connections.IConnectionBuilder Use(System.Func<Microsoft.AspNetCore.Connections.ConnectionDelegate, Microsoft.AspNetCore.Connections.ConnectionDelegate> middleware);
111111
}
112+
public partial interface IConnectionFactory
113+
{
114+
System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Connections.ConnectionContext> ConnectAsync(System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
115+
}
112116
public partial interface IConnectionListener : System.IAsyncDisposable
113117
{
114118
System.Net.EndPoint EndPoint { get; }
@@ -125,6 +129,11 @@ public enum TransferFormat
125129
Binary = 1,
126130
Text = 2,
127131
}
132+
public partial class UriEndPoint : System.Net.EndPoint
133+
{
134+
public UriEndPoint(System.Uri uri) { }
135+
public System.Uri Uri { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
136+
}
128137
}
129138
namespace Microsoft.AspNetCore.Connections.Features
130139
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.Net;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNetCore.Connections
9+
{
10+
/// <summary>
11+
/// A factory abstraction for creating connections to an endpoint.
12+
/// </summary>
13+
public interface IConnectionFactory
14+
{
15+
/// <summary>
16+
/// Creates a new connection to an endpoint.
17+
/// </summary>
18+
/// <param name="endPoint">The <see cref="EndPoint"/> to connect to.</param>
19+
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None" />.</param>
20+
/// <returns>
21+
/// A <see cref="ValueTask{TResult}" /> that represents the asynchronous connect, yielding the <see cref="ConnectionContext" /> for the new connection when completed.
22+
/// </returns>
23+
ValueTask<ConnectionContext> ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken = default);
24+
}
25+
}

src/Servers/Connections.Abstractions/src/IConnectionListenerFactory.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Net;
7-
using System.Text;
85
using System.Threading;
96
using System.Threading.Tasks;
107

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
using System.Net;
6+
7+
namespace Microsoft.AspNetCore.Connections
8+
{
9+
/// <summary>
10+
/// An <see cref="EndPoint"/> defined by a <see cref="System.Uri"/>.
11+
/// </summary>
12+
public class UriEndPoint : EndPoint
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="UriEndPoint"/> class.
16+
/// </summary>
17+
/// <param name="uri">The <see cref="System.Uri"/> defining the <see cref="EndPoint"/>.</param>
18+
public UriEndPoint(Uri uri)
19+
{
20+
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
21+
}
22+
23+
/// <summary>
24+
/// The <see cref="System.Uri"/> defining the <see cref="EndPoint"/>.
25+
/// </summary>
26+
public Uri Uri { get; }
27+
}
28+
}

src/SignalR/clients/csharp/Client.Core/ref/Microsoft.AspNetCore.SignalR.Client.Core.netstandard2.0.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public partial class HubConnection
88
public static readonly System.TimeSpan DefaultHandshakeTimeout;
99
public static readonly System.TimeSpan DefaultKeepAliveInterval;
1010
public static readonly System.TimeSpan DefaultServerTimeout;
11-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
12-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
13-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.SignalR.Client.IRetryPolicy reconnectPolicy) { }
11+
public HubConnection(Microsoft.AspNetCore.Connections.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.Net.EndPoint endPoint, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
12+
public HubConnection(Microsoft.AspNetCore.Connections.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.Net.EndPoint endPoint, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.SignalR.Client.IRetryPolicy reconnectPolicy) { }
1413
public string ConnectionId { get { throw null; } }
1514
public System.TimeSpan HandshakeTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
1615
public System.TimeSpan KeepAliveInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
@@ -145,11 +144,6 @@ public enum HubConnectionState
145144
Connecting = 2,
146145
Reconnecting = 3,
147146
}
148-
public partial interface IConnectionFactory
149-
{
150-
System.Threading.Tasks.Task<Microsoft.AspNetCore.Connections.ConnectionContext> ConnectAsync(Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
151-
System.Threading.Tasks.Task DisposeAsync(Microsoft.AspNetCore.Connections.ConnectionContext connection);
152-
}
153147
public partial interface IHubConnectionBuilder : Microsoft.AspNetCore.SignalR.ISignalRBuilder
154148
{
155149
Microsoft.AspNetCore.SignalR.Client.HubConnection Build();

src/SignalR/clients/csharp/Client.Core/ref/Microsoft.AspNetCore.SignalR.Client.Core.netstandard2.1.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public partial class HubConnection
88
public static readonly System.TimeSpan DefaultHandshakeTimeout;
99
public static readonly System.TimeSpan DefaultKeepAliveInterval;
1010
public static readonly System.TimeSpan DefaultServerTimeout;
11-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
12-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
13-
public HubConnection(Microsoft.AspNetCore.SignalR.Client.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.SignalR.Client.IRetryPolicy reconnectPolicy) { }
11+
public HubConnection(Microsoft.AspNetCore.Connections.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.Net.EndPoint endPoint, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
12+
public HubConnection(Microsoft.AspNetCore.Connections.IConnectionFactory connectionFactory, Microsoft.AspNetCore.SignalR.Protocol.IHubProtocol protocol, System.Net.EndPoint endPoint, System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.SignalR.Client.IRetryPolicy reconnectPolicy) { }
1413
public string ConnectionId { get { throw null; } }
1514
public System.TimeSpan HandshakeTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
1615
public System.TimeSpan KeepAliveInterval { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
@@ -145,11 +144,6 @@ public enum HubConnectionState
145144
Connecting = 2,
146145
Reconnecting = 3,
147146
}
148-
public partial interface IConnectionFactory
149-
{
150-
System.Threading.Tasks.Task<Microsoft.AspNetCore.Connections.ConnectionContext> ConnectAsync(Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
151-
System.Threading.Tasks.Task DisposeAsync(Microsoft.AspNetCore.Connections.ConnectionContext connection);
152-
}
153147
public partial interface IHubConnectionBuilder : Microsoft.AspNetCore.SignalR.ISignalRBuilder
154148
{
155149
Microsoft.AspNetCore.SignalR.Client.HubConnection Build();

src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Globalization;
99
using System.IO;
1010
using System.Linq;
11+
using System.Net;
1112
using System.Reflection;
1213
using System.Runtime.CompilerServices;
1314
using System.Threading;
@@ -21,6 +22,7 @@
2122
using Microsoft.AspNetCore.SignalR.Protocol;
2223
using Microsoft.Extensions.Logging;
2324
using Microsoft.Extensions.Logging.Abstractions;
25+
using Microsoft.Extensions.Options;
2426

2527
namespace Microsoft.AspNetCore.SignalR.Client
2628
{
@@ -59,6 +61,7 @@ public partial class HubConnection
5961
private readonly IServiceProvider _serviceProvider;
6062
private readonly IConnectionFactory _connectionFactory;
6163
private readonly IRetryPolicy _reconnectPolicy;
64+
private readonly EndPoint _endPoint;
6265
private readonly ConcurrentDictionary<string, InvocationHandlerList> _handlers = new ConcurrentDictionary<string, InvocationHandlerList>(StringComparer.Ordinal);
6366

6467
// Holds all mutable state other than user-defined handlers and settable properties.
@@ -172,6 +175,7 @@ public partial class HubConnection
172175
/// </summary>
173176
/// <param name="connectionFactory">The <see cref="IConnectionFactory" /> used to create a connection each time <see cref="StartAsync" /> is called.</param>
174177
/// <param name="protocol">The <see cref="IHubProtocol" /> used by the connection.</param>
178+
/// <param name="endPoint">The <see cref="EndPoint"/> to connect to.</param>
175179
/// <param name="serviceProvider">An <see cref="IServiceProvider"/> containing the services provided to this <see cref="HubConnection"/> instance.</param>
176180
/// <param name="loggerFactory">The logger factory.</param>
177181
/// <param name="reconnectPolicy">
@@ -181,8 +185,8 @@ public partial class HubConnection
181185
/// <remarks>
182186
/// The <see cref="IServiceProvider"/> used to initialize the connection will be disposed when the connection is disposed.
183187
/// </remarks>
184-
public HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, IServiceProvider serviceProvider, ILoggerFactory loggerFactory, IRetryPolicy reconnectPolicy)
185-
: this(connectionFactory, protocol, serviceProvider, loggerFactory)
188+
public HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, EndPoint endPoint, IServiceProvider serviceProvider, ILoggerFactory loggerFactory, IRetryPolicy reconnectPolicy)
189+
: this(connectionFactory, protocol, endPoint, serviceProvider, loggerFactory)
186190
{
187191
_reconnectPolicy = reconnectPolicy;
188192
}
@@ -192,27 +196,22 @@ public HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol
192196
/// </summary>
193197
/// <param name="connectionFactory">The <see cref="IConnectionFactory" /> used to create a connection each time <see cref="StartAsync" /> is called.</param>
194198
/// <param name="protocol">The <see cref="IHubProtocol" /> used by the connection.</param>
199+
/// <param name="endPoint">The <see cref="EndPoint"/> to connect to.</param>
195200
/// <param name="serviceProvider">An <see cref="IServiceProvider"/> containing the services provided to this <see cref="HubConnection"/> instance.</param>
196201
/// <param name="loggerFactory">The logger factory.</param>
197202
/// <remarks>
198203
/// The <see cref="IServiceProvider"/> used to initialize the connection will be disposed when the connection is disposed.
199204
/// </remarks>
200-
public HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
201-
: this(connectionFactory, protocol, loggerFactory)
202-
{
203-
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
204-
}
205-
206-
/// <summary>
207-
/// Initializes a new instance of the <see cref="HubConnection"/> class.
208-
/// </summary>
209-
/// <param name="connectionFactory">The <see cref="IConnectionFactory" /> used to create a connection each time <see cref="StartAsync" /> is called.</param>
210-
/// <param name="protocol">The <see cref="IHubProtocol" /> used by the connection.</param>
211-
/// <param name="loggerFactory">The logger factory.</param>
212-
public HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, ILoggerFactory loggerFactory)
205+
public HubConnection(IConnectionFactory connectionFactory,
206+
IHubProtocol protocol,
207+
EndPoint endPoint,
208+
IServiceProvider serviceProvider,
209+
ILoggerFactory loggerFactory)
213210
{
214211
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
215212
_protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
213+
_endPoint = endPoint ?? throw new ArgumentException(nameof(endPoint));
214+
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
216215

217216
_loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
218217
_logger = _loggerFactory.CreateLogger<HubConnection>();
@@ -424,7 +423,7 @@ private async Task StartAsyncCore(CancellationToken cancellationToken)
424423
Log.Starting(_logger);
425424

426425
// Start the connection
427-
var connection = await _connectionFactory.ConnectAsync(_protocol.TransferFormat, cancellationToken);
426+
var connection = await _connectionFactory.ConnectAsync(_endPoint, cancellationToken);
428427
var startingConnectionState = new ConnectionState(connection, this);
429428

430429
// From here on, if an error occurs we need to shut down the connection because
@@ -450,9 +449,9 @@ private async Task StartAsyncCore(CancellationToken cancellationToken)
450449
Log.Started(_logger);
451450
}
452451

453-
private Task CloseAsync(ConnectionContext connection)
452+
private ValueTask CloseAsync(ConnectionContext connection)
454453
{
455-
return _connectionFactory.DisposeAsync(connection);
454+
return connection.DisposeAsync();
456455
}
457456

458457
// This method does both Dispose and Start, the 'disposing' flag indicates which.

src/SignalR/clients/csharp/Client.Core/src/HubConnectionBuilder.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
using System;
55
using System.ComponentModel;
6+
using System.Linq;
7+
using System.Net;
8+
using Microsoft.AspNetCore.Connections;
69
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Options;
711

812
namespace Microsoft.AspNetCore.SignalR.Client
913
{
@@ -42,11 +46,11 @@ public HubConnection Build()
4246
// The service provider is disposed by the HubConnection
4347
var serviceProvider = Services.BuildServiceProvider();
4448

45-
var connectionFactory = serviceProvider.GetService<IConnectionFactory>();
46-
if (connectionFactory == null)
47-
{
49+
var connectionFactory = serviceProvider.GetService<IConnectionFactory>() ??
4850
throw new InvalidOperationException($"Cannot create {nameof(HubConnection)} instance. An {nameof(IConnectionFactory)} was not configured.");
49-
}
51+
52+
var endPoint = serviceProvider.GetService<EndPoint>() ??
53+
throw new InvalidOperationException($"Cannot create {nameof(HubConnection)} instance. An {nameof(EndPoint)} was not configured.");
5054

5155
return serviceProvider.GetService<HubConnection>();
5256
}

src/SignalR/clients/csharp/Client.Core/src/IConnectionFactory.cs

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

src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Client for ASP.NET Core SignalR</Description>

0 commit comments

Comments
 (0)