diff --git a/samples/ChatSample/Views/Home/Index.cshtml b/samples/ChatSample/Views/Home/Index.cshtml index 0207c365e6..6089f6a0af 100644 --- a/samples/ChatSample/Views/Home/Index.cshtml +++ b/samples/ChatSample/Views/Home/Index.cshtml @@ -35,15 +35,15 @@ connection.on('SetUsersOnline', usersOnline => { connection.on('UsersJoined', users => { users.forEach(user => { - appendLine('User ' + user.Name + ' joined the chat'); + appendLine('User ' + user.name + ' joined the chat'); addUserOnline(user); }); }); connection.on('UsersLeft', users => { users.forEach(user => { - appendLine('User ' + user.Name + ' left the chat'); - document.getElementById(user.ConnectionId).outerHTML = ''; + appendLine('User ' + user.name + ' left the chat'); + document.getElementById(user.connectionId).outerHTML = ''; }); }); @@ -78,12 +78,12 @@ function appendLine(line, color) { }; function addUserOnline(user) { - if (document.getElementById(user.ConnectionId)) { + if (document.getElementById(user.connectionId)) { return; } var userLi = document.createElement('li'); - userLi.innerText = `${user.Name} (${user.ConnectionId})`; - userLi.id = user.ConnectionId; + userLi.innerText = `${user.name} (${user.connectionId})`; + userLi.id = user.connectionId; document.getElementById('users').appendChild(userLi); } diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs index a4fc7cbe88..fb5aea9f8f 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/JsonHubProtocol.cs @@ -7,6 +7,7 @@ using System.IO; using Microsoft.AspNetCore.SignalR.Internal.Formatters; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; namespace Microsoft.AspNetCore.SignalR.Internal.Protocol { @@ -34,7 +35,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol /// NOT be changed using this serializer. /// public JsonHubProtocol() - : this(new JsonSerializer()) + : this(JsonSerializer.Create(CreateDefaultSerializerSettings())) { } /// @@ -258,5 +259,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol return new CompletionMessage(invocationId, error, result: payload, hasResult: true); } } + + public static JsonSerializerSettings CreateDefaultSerializerSettings() + { + return new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; + } } } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs index b1eebb66fd..f652ea6755 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.SignalR { public class HubOptions { - public JsonSerializerSettings JsonSerializerSettings { get; set; } = new JsonSerializerSettings(); + public JsonSerializerSettings JsonSerializerSettings { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings(); public SerializationContext MessagePackSerializationContext { get; set; } = MessagePackHubProtocol.CreateDefaultSerializationContext(); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs index 104c32739c..673c3bd1bf 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs @@ -1008,7 +1008,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests { o.JsonSerializerSettings = new JsonSerializerSettings { - ContractResolver = new CamelCasePropertyNamesContractResolver() + ContractResolver = new DefaultContractResolver() }; }); }); @@ -1026,7 +1026,34 @@ namespace Microsoft.AspNetCore.SignalR.Tests var message = (InvocationMessage)await client.ReadAsync().OrTimeout(); var customItem = message.Arguments[0].ToString(); - // Originally "Message" and "paramName" + // by default properties serialized by JsonHubProtocol are using camelCasing + Assert.Contains("Message", customItem); + Assert.Contains("paramName", customItem); + + client.Dispose(); + + await endPointLifetime.OrTimeout(); + } + } + + [Fact] + public async Task JsonHubProtocolUsesCamelCasingByDefault() + { + var serviceProvider = CreateServiceProvider(); + var endPoint = serviceProvider.GetService>(); + + using (var client = new TestClient()) + { + var endPointLifetime = endPoint.OnConnectedAsync(client.Connection); + + await client.Connected.OrTimeout(); + + await client.SendInvocationAsync(nameof(MethodHub.BroadcastItem)).OrTimeout(); + + var message = (InvocationMessage)await client.ReadAsync().OrTimeout(); + + var customItem = message.Arguments[0].ToString(); + // originally Message, paramName Assert.Contains("message", customItem); Assert.Contains("paramName", customItem);