Skip to content

Commit 998cc42

Browse files
committed
Changes per PR feedback
1 parent cfbfbd7 commit 998cc42

File tree

10 files changed

+78
-36
lines changed

10 files changed

+78
-36
lines changed

src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ internal static HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConne
9393
AccessTokenProvider = options.AccessTokenProvider,
9494
CloseTimeout = options.CloseTimeout,
9595
DefaultTransferFormat = options.DefaultTransferFormat,
96-
WebSocketConfiguration = options.WebSocketConfiguration,
9796
};
9897

9998
if (!OperatingSystem.IsBrowser())
@@ -103,6 +102,7 @@ internal static HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConne
103102
newOptions.Credentials = options.Credentials;
104103
newOptions.Proxy = options.Proxy;
105104
newOptions.UseDefaultCredentials = options.UseDefaultCredentials;
105+
newOptions.WebSocketConfiguration = options.WebSocketConfiguration;
106106
}
107107

108108
return newOptions;

src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnectionOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class HttpConnectionOptions
2626
private ICredentials _credentials;
2727
private IWebProxy _proxy;
2828
private bool? _useDefaultCredentials;
29+
private Action<ClientWebSocketOptions> _webSocketConfiguration;
2930

3031
/// <summary>
3132
/// Initializes a new instance of the <see cref="HttpConnectionOptions"/> class.
@@ -192,7 +193,20 @@ public bool? UseDefaultCredentials
192193
/// This delegate is invoked after headers from <see cref="Headers"/> and the access token from <see cref="AccessTokenProvider"/>
193194
/// has been applied.
194195
/// </remarks>
195-
public Action<ClientWebSocketOptions> WebSocketConfiguration { get; set; }
196+
[UnsupportedOSPlatform("browser")]
197+
public Action<ClientWebSocketOptions> WebSocketConfiguration
198+
{
199+
get
200+
{
201+
ThrowIfUnsupportedPlatform();
202+
return _webSocketConfiguration;
203+
}
204+
set
205+
{
206+
ThrowIfUnsupportedPlatform();
207+
_webSocketConfiguration = value;
208+
}
209+
}
196210

197211
private static void ThrowIfUnsupportedPlatform()
198212
{

src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.Log.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ private static class Log
6969
private static readonly Action<ILogger, Exception> _startedTransport =
7070
LoggerMessage.Define(LogLevel.Debug, new EventId(19, "StartedTransport"), "Started transport.");
7171

72+
private static readonly Action<ILogger, Exception> _headersNotSupported =
73+
LoggerMessage.Define(LogLevel.Warning, new EventId(20, "HeadersNotSupported"),
74+
$"Configuring request headers using {nameof(HttpConnectionOptions)}.{nameof(HttpConnectionOptions.Headers)} is not supported when using websockets transport " +
75+
"on the browser platform.");
76+
77+
7278
public static void StartTransport(ILogger logger, TransferFormat transferFormat, Uri webSocketUrl)
7379
{
7480
_startTransport(logger, transferFormat, webSocketUrl, null);
@@ -163,6 +169,11 @@ public static void StartedTransport(ILogger logger)
163169
{
164170
_startedTransport(logger, null);
165171
}
172+
173+
public static void HeadersNotSupported(ILogger logger)
174+
{
175+
_headersNotSupported(logger, null);
176+
}
166177
}
167178
}
168179
}

src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ public WebSocketsTransport(HttpConnectionOptions httpConnectionOptions, ILoggerF
5151

5252
if (httpConnectionOptions != null)
5353
{
54-
if (httpConnectionOptions.Headers != null)
54+
if (httpConnectionOptions.Headers.Count > 0)
5555
{
5656
if (isBrowser)
5757
{
58-
throw new PlatformNotSupportedException("Configuring request headers is not supported when using WebSocketsTransport in the browser platform.");
58+
Log.HeadersNotSupported(_logger);
5959
}
60-
61-
foreach (var header in httpConnectionOptions.Headers)
60+
else
6261
{
63-
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
62+
foreach (var header in httpConnectionOptions.Headers)
63+
{
64+
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
65+
}
6466
}
6567
}
6668

@@ -90,9 +92,9 @@ public WebSocketsTransport(HttpConnectionOptions httpConnectionOptions, ILoggerF
9092
{
9193
_webSocket.Options.UseDefaultCredentials = httpConnectionOptions.UseDefaultCredentials.Value;
9294
}
93-
}
9495

95-
httpConnectionOptions.WebSocketConfiguration?.Invoke(_webSocket.Options);
96+
httpConnectionOptions.WebSocketConfiguration?.Invoke(_webSocket.Options);
97+
}
9698
}
9799

98100
if (!isBrowser)
@@ -101,7 +103,7 @@ public WebSocketsTransport(HttpConnectionOptions httpConnectionOptions, ILoggerF
101103
// See: https://github.com/aspnet/Security/blob/ff9f145a8e89c9756ea12ff10c6d47f2f7eb345f/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs#L42
102104
#pragma warning disable CA1416 // Analyzer bug
103105
_webSocket.Options.SetRequestHeader("X-Requested-With", "XMLHttpRequest");
104-
#pragma warning restore CA1416 // Validate platform compatibility
106+
#pragma warning restore CA1416 // Analyzer bug
105107
}
106108

107109
_closeTimeout = httpConnectionOptions?.CloseTimeout ?? default;

src/SignalR/common/Protocols.MessagePack/src/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Implements the SignalR Hub Protocol over MsgPack.</Description>
5-
<TargetFrameworks>$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks>$(DefaultNetCoreTargetFramework);$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
66
<RootNamespace>Microsoft.AspNetCore.SignalR</RootNamespace>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<Nullable>enable</Nullable>

src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocolWorker.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal abstract class MessagePackHubProtocolWorker
2121
private const int VoidResult = 2;
2222
private const int NonVoidResult = 3;
2323

24-
public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder binder, out HubMessage message)
24+
public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder binder, out HubMessage? message)
2525
{
2626
if (!BinaryMessageParser.TryParseMessage(ref input, out var payload))
2727
{
@@ -34,7 +34,7 @@ public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder
3434
return true;
3535
}
3636

37-
private HubMessage ParseMessage(ref MessagePackReader reader, IInvocationBinder binder)
37+
private HubMessage? ParseMessage(ref MessagePackReader reader, IInvocationBinder binder)
3838
{
3939
var itemCount = reader.ReadArrayHeader();
4040

@@ -76,7 +76,7 @@ private HubMessage CreateInvocationMessage(ref MessagePackReader reader, IInvoca
7676

7777
var target = ReadString(ref reader, "target");
7878

79-
object[] arguments = null;
79+
object[]? arguments;
8080
try
8181
{
8282
var parameterTypes = binder.GetParameterTypes(target);
@@ -87,7 +87,7 @@ private HubMessage CreateInvocationMessage(ref MessagePackReader reader, IInvoca
8787
return new InvocationBindingFailureMessage(invocationId, target, ExceptionDispatchInfo.Capture(ex));
8888
}
8989

90-
string[] streams = null;
90+
string[]? streams = null;
9191
// Previous clients will send 5 items, so we check if they sent a stream array or not
9292
if (itemCount > 5)
9393
{
@@ -103,7 +103,7 @@ private HubMessage CreateStreamInvocationMessage(ref MessagePackReader reader, I
103103
var invocationId = ReadInvocationId(ref reader);
104104
var target = ReadString(ref reader, "target");
105105

106-
object[] arguments = null;
106+
object[] arguments;
107107
try
108108
{
109109
var parameterTypes = binder.GetParameterTypes(target);
@@ -114,7 +114,7 @@ private HubMessage CreateStreamInvocationMessage(ref MessagePackReader reader, I
114114
return new InvocationBindingFailureMessage(invocationId, target, ExceptionDispatchInfo.Capture(ex));
115115
}
116116

117-
string[] streams = null;
117+
string[]? streams = null;
118118
// Previous clients will send 5 items, so we check if they sent a stream array or not
119119
if (itemCount > 5)
120120
{
@@ -148,8 +148,8 @@ private CompletionMessage CreateCompletionMessage(ref MessagePackReader reader,
148148
var invocationId = ReadInvocationId(ref reader);
149149
var resultKind = ReadInt32(ref reader, "resultKind");
150150

151-
string error = null;
152-
object result = null;
151+
string? error = null;
152+
object? result = null;
153153
var hasResult = false;
154154

155155
switch (resultKind)
@@ -198,7 +198,7 @@ private CloseMessage CreateCloseMessage(ref MessagePackReader reader, int itemCo
198198
return new CloseMessage(error, allowReconnect);
199199
}
200200

201-
private Dictionary<string, string> ReadHeaders(ref MessagePackReader reader)
201+
private Dictionary<string, string>? ReadHeaders(ref MessagePackReader reader)
202202
{
203203
var headerCount = ReadMapLength(ref reader, "headers");
204204
if (headerCount > 0)
@@ -219,10 +219,10 @@ private Dictionary<string, string> ReadHeaders(ref MessagePackReader reader)
219219
}
220220
}
221221

222-
private string[] ReadStreamIds(ref MessagePackReader reader)
222+
private string[]? ReadStreamIds(ref MessagePackReader reader)
223223
{
224224
var streamIdCount = ReadArrayLength(ref reader, "streamIds");
225-
List<string> streams = null;
225+
List<string>? streams = null;
226226

227227
if (streamIdCount > 0)
228228
{
@@ -264,7 +264,7 @@ private object[] BindArguments(ref MessagePackReader reader, IReadOnlyList<Type>
264264

265265
protected abstract object DeserializeObject(ref MessagePackReader reader, Type type, string field);
266266

267-
private T ApplyHeaders<T>(IDictionary<string, string> source, T destination) where T : HubInvocationMessage
267+
private T ApplyHeaders<T>(IDictionary<string, string>? source, T destination) where T : HubInvocationMessage
268268
{
269269
if (source != null && source.Count > 0)
270270
{
@@ -374,10 +374,18 @@ private void WriteInvocationMessage(InvocationMessage message, ref MessagePackWr
374374
writer.Write(message.InvocationId);
375375
}
376376
writer.Write(message.Target);
377-
writer.WriteArrayHeader(message.Arguments.Length);
378-
foreach (var arg in message.Arguments)
377+
378+
if (message.Arguments is null)
379379
{
380-
WriteArgument(arg, ref writer);
380+
writer.WriteArrayHeader(0);
381+
}
382+
else
383+
{
384+
writer.WriteArrayHeader(message.Arguments.Length);
385+
foreach (var arg in message.Arguments)
386+
{
387+
WriteArgument(arg, ref writer);
388+
}
381389
}
382390

383391
WriteStreamIds(message.StreamIds, ref writer);
@@ -392,10 +400,17 @@ private void WriteStreamInvocationMessage(StreamInvocationMessage message, ref M
392400
writer.Write(message.InvocationId);
393401
writer.Write(message.Target);
394402

395-
writer.WriteArrayHeader(message.Arguments.Length);
396-
foreach (var arg in message.Arguments)
403+
if (message.Arguments is null)
397404
{
398-
WriteArgument(arg, ref writer);
405+
writer.WriteArrayHeader(0);
406+
}
407+
else
408+
{
409+
writer.WriteArrayHeader(message.Arguments.Length);
410+
foreach (var arg in message.Arguments)
411+
{
412+
WriteArgument(arg, ref writer);
413+
}
399414
}
400415

401416
WriteStreamIds(message.StreamIds, ref writer);
@@ -410,7 +425,7 @@ private void WriteStreamingItemMessage(StreamItemMessage message, ref MessagePac
410425
WriteArgument(message.Item, ref writer);
411426
}
412427

413-
private void WriteArgument(object argument, ref MessagePackWriter writer)
428+
private void WriteArgument(object? argument, ref MessagePackWriter writer)
414429
{
415430
if (argument == null)
416431
{
@@ -424,7 +439,7 @@ private void WriteArgument(object argument, ref MessagePackWriter writer)
424439

425440
protected abstract void Serialize(ref MessagePackWriter writer, Type type, object value);
426441

427-
private void WriteStreamIds(string[] streamIds, ref MessagePackWriter writer)
442+
private void WriteStreamIds(string[]? streamIds, ref MessagePackWriter writer)
428443
{
429444
if (streamIds != null)
430445
{
@@ -493,7 +508,7 @@ private void WritePingMessage(PingMessage pingMessage, ref MessagePackWriter wri
493508
writer.Write(HubProtocolConstants.PingMessageType);
494509
}
495510

496-
private void PackHeaders(IDictionary<string, string> headers, ref MessagePackWriter writer)
511+
private void PackHeaders(IDictionary<string, string>? headers, ref MessagePackWriter writer)
497512
{
498513
if (headers != null)
499514
{

src/SignalR/common/Protocols.NewtonsoftJson/src/Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Implements the SignalR Hub Protocol using Newtonsoft.Json.</Description>
5-
<TargetFrameworks>$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks>$(DefaultNetCoreTargetFramework);$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
66
<RootNamespace>Microsoft.AspNetCore.SignalR</RootNamespace>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<Nullable>enable</Nullable>

src/SignalR/common/Shared/JsonUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static bool ReadAsBoolean(JsonTextReader reader, string propertyName)
143143
return Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
144144
}
145145

146-
public static string ReadAsString(JsonTextReader reader, string propertyName)
146+
public static string? ReadAsString(JsonTextReader reader, string propertyName)
147147
{
148148
reader.Read();
149149

src/SignalR/common/SignalR.Common/src/Protocol/HubMethodInvocationMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public StreamInvocationMessage(string invocationId, string target, object[] argu
146146
/// <param name="target">The target method name.</param>
147147
/// <param name="arguments">The target method arguments.</param>
148148
/// <param name="streamIds">The target methods stream IDs.</param>
149-
public StreamInvocationMessage(string invocationId, string target, object[] arguments, string[] streamIds)
149+
public StreamInvocationMessage(string invocationId, string target, object[] arguments, string[]? streamIds)
150150
: base(invocationId, target, arguments, streamIds)
151151
{
152152
}

src/SignalR/common/SignalR.Common/src/Protocol/InvocationBindingFailureMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class InvocationBindingFailureMessage : HubInvocationMessage
2626
/// <param name="invocationId">The invocation ID.</param>
2727
/// <param name="target">The target method name.</param>
2828
/// <param name="bindingFailure">The exception thrown during binding.</param>
29-
public InvocationBindingFailureMessage(string invocationId, string target, ExceptionDispatchInfo bindingFailure) : base(invocationId)
29+
public InvocationBindingFailureMessage(string? invocationId, string target, ExceptionDispatchInfo bindingFailure) : base(invocationId)
3030
{
3131
Target = target;
3232
BindingFailure = bindingFailure;

0 commit comments

Comments
 (0)