From c01c8dd47624326a452b8e6ae5f85eddae36f30d Mon Sep 17 00:00:00 2001 From: TeBeCo Date: Fri, 3 Apr 2020 02:41:40 +0200 Subject: [PATCH] Changing msgpack options (#20031) --- .../src/MessagePackHubProtocolOptions.cs | 19 +++++--- .../src/Protocol/MessagePackHubProtocol.cs | 45 ++++--------------- .../SignalR/test/HubConnectionHandlerTests.cs | 3 +- 3 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs b/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs index 85025b6444..bd2ad8f788 100644 --- a/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs +++ b/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs @@ -9,24 +9,31 @@ namespace Microsoft.AspNetCore.SignalR { public class MessagePackHubProtocolOptions { - private IList _formatterResolvers; + private MessagePackSerializerOptions _messagePackSerializerOptions; - public IList FormatterResolvers + /// + /// Gets or sets the used internally by the . + /// If you override the default value, we strongly recommend that you set to by calling: + /// customMessagePackSerializerOptions = customMessagePackSerializerOptions.WithSecurity(MessagePackSecurity.UntrustedData) + /// If you modify the default options you must also assign the updated options back to the property: + /// options.SerializerOptions = options.SerializerOptions.WithResolver(new CustomResolver()); + /// + public MessagePackSerializerOptions SerializerOptions { get { - if (_formatterResolvers == null) + if (_messagePackSerializerOptions == null) { // The default set of resolvers trigger a static constructor that throws on AOT environments. // This gives users the chance to use an AOT friendly formatter. - _formatterResolvers = MessagePackHubProtocol.CreateDefaultFormatterResolvers(); + _messagePackSerializerOptions = MessagePackHubProtocol.CreateDefaultMessagePackSerializerOptions(); } - return _formatterResolvers; + return _messagePackSerializerOptions; } set { - _formatterResolvers = value; + _messagePackSerializerOptions = value; } } } diff --git a/src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocol.cs b/src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocol.cs index e1a762937b..ffa814da22 100644 --- a/src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocol.cs +++ b/src/SignalR/common/Protocols.MessagePack/src/Protocol/MessagePackHubProtocol.cs @@ -27,6 +27,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol private const int NonVoidResult = 3; private readonly MessagePackSerializerOptions _msgPackSerializerOptions; + private static readonly string ProtocolName = "messagepack"; private static readonly int ProtocolVersion = 1; @@ -52,37 +53,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol /// The options used to initialize the protocol. public MessagePackHubProtocol(IOptions options) { - var msgPackOptions = options.Value; - var resolver = SignalRResolver.Instance; - var hasCustomFormatterResolver = false; - - // if counts don't match then we know users customized resolvers so we set up the options with the provided resolvers - if (msgPackOptions.FormatterResolvers.Count != SignalRResolver.Resolvers.Count) - { - hasCustomFormatterResolver = true; - } - else - { - // Compare each "reference" in the FormatterResolvers IList<> against the default "SignalRResolver.Resolvers" IList<> - for (var i = 0; i < msgPackOptions.FormatterResolvers.Count; i++) - { - // check if the user customized the resolvers - if (msgPackOptions.FormatterResolvers[i] != SignalRResolver.Resolvers[i]) - { - hasCustomFormatterResolver = true; - break; - } - } - } - - if (hasCustomFormatterResolver) - { - resolver = CompositeResolver.Create(Array.Empty(), (IReadOnlyList)msgPackOptions.FormatterResolvers); - } - - _msgPackSerializerOptions = MessagePackSerializerOptions.Standard - .WithResolver(resolver) - .WithSecurity(MessagePackSecurity.UntrustedData); + _msgPackSerializerOptions = options.Value.SerializerOptions; } /// @@ -656,17 +627,17 @@ namespace Microsoft.AspNetCore.SignalR.Protocol } } - internal static List CreateDefaultFormatterResolvers() - { - // Copy to allow users to add/remove resolvers without changing the static SignalRResolver list - return new List(SignalRResolver.Resolvers); - } + internal static MessagePackSerializerOptions CreateDefaultMessagePackSerializerOptions() => + MessagePackSerializerOptions + .Standard + .WithResolver(SignalRResolver.Instance) + .WithSecurity(MessagePackSecurity.UntrustedData); internal class SignalRResolver : IFormatterResolver { public static readonly IFormatterResolver Instance = new SignalRResolver(); - public static readonly IList Resolvers = new IFormatterResolver[] + public static readonly IReadOnlyList Resolvers = new IFormatterResolver[] { DynamicEnumAsStringResolver.Instance, ContractlessStandardResolver.Instance, diff --git a/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs b/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs index 4439cf3bb9..d65de0ba1f 100644 --- a/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs +++ b/src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs @@ -14,6 +14,7 @@ using System.Threading; using System.Threading.Tasks; using MessagePack; using MessagePack.Formatters; +using MessagePack.Resolvers; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http; @@ -2371,7 +2372,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests services.AddSignalR() .AddMessagePackProtocol(options => { - options.FormatterResolvers.Insert(0, new CustomFormatter()); + options.SerializerOptions = MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new CustomFormatter(), options.SerializerOptions.Resolver)); }); }, LoggerFactory);