Skip to content

Commit 6ecd65d

Browse files
authored
Expose an option for the netframework to configure the underlying websocket options (#2180)
1 parent 7d71fd0 commit 6ecd65d

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

src/Microsoft.Azure.SignalR.AspNet/DispatcherHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ internal static ServiceHubDispatcher PrepareAndGetDispatcher(IAppBuilder builder
132132
var cf = configuration.Resolver.Resolve<IConnectionFactory>();
133133
if (cf == null)
134134
{
135-
cf = new ConnectionFactory(serverNameProvider, loggerFactory);
135+
cf = new ConnectionFactory(serverNameProvider, loggerFactory)
136+
{
137+
ConfigureServiceConnectionWebSocketOptions = options.ConfigureServiceConnectionWebSocketOptions
138+
};
136139
configuration.Resolver.Register(typeof(IConnectionFactory), () => cf);
137140
}
138141

src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Owin;
1212

1313
namespace Microsoft.Azure.SignalR.AspNet;
14+
#nullable enable
1415

1516
/// <summary>
1617
/// Configurable options when using Azure SignalR Service.
@@ -20,7 +21,7 @@ public class ServiceOptions : IServiceEndpointOptions
2021
public ServiceOptions()
2122
{
2223
var count = ConfigurationManager.ConnectionStrings.Count;
23-
string connectionString = null;
24+
string? connectionString = null;
2425
var endpoints = new List<ServiceEndpoint>();
2526
var connectionStringKeyPrefix = $"{Constants.Keys.ConnectionStringDefaultKey}:";
2627
for (var i = 0; i < count; i++)
@@ -82,7 +83,7 @@ public ServiceOptions()
8283
/// <summary>
8384
/// Gets or sets the proxy used when ServiceEndpoint will attempt to connect to Azure SignalR.
8485
/// </summary>
85-
public IWebProxy Proxy { get; set; }
86+
public IWebProxy? Proxy { get; set; }
8687

8788
/// <summary>
8889
/// Specifies the mode for server sticky, when client is always routed to the server which it first /negotiate with, we call it "server sticky mode".
@@ -96,7 +97,14 @@ public ServiceOptions()
9697
/// Gets or sets the func to generate claims from <see cref="IOwinContext" />.
9798
/// The claims will be included in the auto-generated token for clients.
9899
/// </summary>
99-
public Func<IOwinContext, IEnumerable<Claim>> ClaimsProvider { get; set; } = null;
100+
public Func<IOwinContext, IEnumerable<Claim>>? ClaimsProvider { get; set; }
101+
102+
/// <summary>
103+
/// Provide the ability to configure the underlying ClientWebSocketOptions for the WebSocketClient used to establish the server connection to the service
104+
/// For example, you could specify the inner buffer used for each WebSocketClient by setting:
105+
/// <code>options.ConfigureServiceConnectionWebSocketOptions = o => o.SetBuffer(0x1000, 0x1000);</code>
106+
/// </summary>
107+
public Action<System.Net.WebSockets.ClientWebSocketOptions>? ConfigureServiceConnectionWebSocketOptions { get; set; }
100108

101109
/// <summary>
102110
/// Gets or sets the initial number of connections per hub from SDK to Azure SignalR Service. Default value is 5.
@@ -114,12 +122,12 @@ public int ConnectionCount
114122
/// <summary>
115123
/// Gets or sets the connection string of Azure SignalR Service instance.
116124
/// </summary>
117-
public string ConnectionString { get; set; }
125+
public string? ConnectionString { get; set; }
118126
/// <summary>
119127
/// Gets or sets the func to set diagnostic client filter from <see cref="IOwinContext" />.
120128
/// The clients will be regarded as diagnostic client only if the function returns true.
121129
/// </summary>
122-
public Func<IOwinContext, bool> DiagnosticClientFilter { get; set; } = null;
130+
public Func<IOwinContext, bool>? DiagnosticClientFilter { get; set; } = null;
123131

124132
/// <summary>
125133
/// Customize the multiple endpoints used
@@ -145,5 +153,5 @@ public int ConnectionCount
145153
/// <summary>
146154
/// Gets applicationName, which will be used as a prefix to apply to each hub name
147155
/// </summary>
148-
internal string ApplicationName { get; set; }
156+
internal string ApplicationName { get; set; } = string.Empty;
149157
}

src/Microsoft.Azure.SignalR.Common/ServiceConnections/ConnectionFactoryBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ internal abstract class ConnectionFactoryBase : IConnectionFactory
2121

2222
private readonly string _serverId;
2323

24+
public Action<System.Net.WebSockets.ClientWebSocketOptions>? ConfigureServiceConnectionWebSocketOptions { get; init; }
25+
2426
public ConnectionFactoryBase(IServerNameProvider nameProvider,
2527
ILoggerFactory loggerFactory)
2628
{
@@ -44,6 +46,7 @@ public async Task<ConnectionContext> ConnectAsync(HubServiceEndpoint hubServiceE
4446
{
4547
Headers = GetRequestHeaders(),
4648
Proxy = provider.Proxy,
49+
WebSocketConfiguration = ConfigureServiceConnectionWebSocketOptions,
4750
};
4851
var connection = new WebSocketConnectionContext(connectionOptions, _loggerFactory, accessTokenProvider);
4952

test/Microsoft.Azure.SignalR.AspNet.Tests/RunAzureSignalRTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,23 @@ public async Task TestRunAzureSignalStartsServerConnection()
844844
using (var logCollector = StartVerifiableLog(out var loggerFactory, LogLevel.Debug))
845845
using (new AppSettingsConfigScope(ConnectionString))
846846
{
847+
var logger = loggerFactory.CreateLogger(nameof(TestRunAzureSignalStartsServerConnection));
847848
var hubConfig = Utility.GetTestHubConfig(loggerFactory);
848849
var hubName = "hub";
849850
var testHub = new TestHubManager(hubName);
850851
hubConfig.Resolver.Register(typeof(IHubManager), () => testHub);
852+
var bufferSize = 0x100;
853+
var bufferLog = $"Buffer set to {bufferSize}";
851854
using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig,
852-
o => o.InitialHubServerConnectionCount = 1
855+
o =>
856+
{
857+
o.InitialHubServerConnectionCount = 1;
858+
o.ConfigureServiceConnectionWebSocketOptions = i =>
859+
{
860+
i.SetBuffer(bufferSize, bufferSize);
861+
logger.LogInformation(bufferLog);
862+
};
863+
}
853864
)))
854865
{
855866
var options = hubConfig.Resolver.Resolve<IServiceConnectionManager>();
@@ -863,6 +874,7 @@ public async Task TestRunAzureSignalStartsServerConnection()
863874
logCollector.Expects("StartTransport");
864875
logCollector.Expects("TransportStopping");
865876
logCollector.Expects("FailedToConnect");
877+
logCollector.Expects(i => i.Write.Message == bufferLog);
866878
}
867879
}
868880

0 commit comments

Comments
 (0)