Skip to content

Commit c01c8dd

Browse files
authored
Changing msgpack options (dotnet#20031)
1 parent f17520c commit c01c8dd

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ namespace Microsoft.AspNetCore.SignalR
99
{
1010
public class MessagePackHubProtocolOptions
1111
{
12-
private IList<IFormatterResolver> _formatterResolvers;
12+
private MessagePackSerializerOptions _messagePackSerializerOptions;
1313

14-
public IList<IFormatterResolver> FormatterResolvers
14+
/// <summary>
15+
/// <para>Gets or sets the <see cref="MessagePackSerializerOptions"/> used internally by the <see cref="MessagePackSerializer" />.</para>
16+
/// <para>If you override the default value, we strongly recommend that you set <see cref="MessagePackSecurity" /> to <see cref="MessagePackSecurity.UntrustedData"/> by calling:</para>
17+
/// <code>customMessagePackSerializerOptions = customMessagePackSerializerOptions.WithSecurity(MessagePackSecurity.UntrustedData)</code>
18+
/// If you modify the default options you must also assign the updated options back to the <see cref="SerializerOptions" /> property:
19+
/// <code>options.SerializerOptions = options.SerializerOptions.WithResolver(new CustomResolver());</code>
20+
/// </summary>
21+
public MessagePackSerializerOptions SerializerOptions
1522
{
1623
get
1724
{
18-
if (_formatterResolvers == null)
25+
if (_messagePackSerializerOptions == null)
1926
{
2027
// The default set of resolvers trigger a static constructor that throws on AOT environments.
2128
// This gives users the chance to use an AOT friendly formatter.
22-
_formatterResolvers = MessagePackHubProtocol.CreateDefaultFormatterResolvers();
29+
_messagePackSerializerOptions = MessagePackHubProtocol.CreateDefaultMessagePackSerializerOptions();
2330
}
2431

25-
return _formatterResolvers;
32+
return _messagePackSerializerOptions;
2633
}
2734
set
2835
{
29-
_formatterResolvers = value;
36+
_messagePackSerializerOptions = value;
3037
}
3138
}
3239
}

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

+8-37
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class MessagePackHubProtocol : IHubProtocol
2727
private const int NonVoidResult = 3;
2828

2929
private readonly MessagePackSerializerOptions _msgPackSerializerOptions;
30+
3031
private static readonly string ProtocolName = "messagepack";
3132
private static readonly int ProtocolVersion = 1;
3233

@@ -52,37 +53,7 @@ public MessagePackHubProtocol()
5253
/// <param name="options">The options used to initialize the protocol.</param>
5354
public MessagePackHubProtocol(IOptions<MessagePackHubProtocolOptions> options)
5455
{
55-
var msgPackOptions = options.Value;
56-
var resolver = SignalRResolver.Instance;
57-
var hasCustomFormatterResolver = false;
58-
59-
// if counts don't match then we know users customized resolvers so we set up the options with the provided resolvers
60-
if (msgPackOptions.FormatterResolvers.Count != SignalRResolver.Resolvers.Count)
61-
{
62-
hasCustomFormatterResolver = true;
63-
}
64-
else
65-
{
66-
// Compare each "reference" in the FormatterResolvers IList<> against the default "SignalRResolver.Resolvers" IList<>
67-
for (var i = 0; i < msgPackOptions.FormatterResolvers.Count; i++)
68-
{
69-
// check if the user customized the resolvers
70-
if (msgPackOptions.FormatterResolvers[i] != SignalRResolver.Resolvers[i])
71-
{
72-
hasCustomFormatterResolver = true;
73-
break;
74-
}
75-
}
76-
}
77-
78-
if (hasCustomFormatterResolver)
79-
{
80-
resolver = CompositeResolver.Create(Array.Empty<IMessagePackFormatter>(), (IReadOnlyList<IFormatterResolver>)msgPackOptions.FormatterResolvers);
81-
}
82-
83-
_msgPackSerializerOptions = MessagePackSerializerOptions.Standard
84-
.WithResolver(resolver)
85-
.WithSecurity(MessagePackSecurity.UntrustedData);
56+
_msgPackSerializerOptions = options.Value.SerializerOptions;
8657
}
8758

8859
/// <inheritdoc />
@@ -656,17 +627,17 @@ private static object DeserializeObject(ref MessagePackReader reader, Type type,
656627
}
657628
}
658629

659-
internal static List<IFormatterResolver> CreateDefaultFormatterResolvers()
660-
{
661-
// Copy to allow users to add/remove resolvers without changing the static SignalRResolver list
662-
return new List<IFormatterResolver>(SignalRResolver.Resolvers);
663-
}
630+
internal static MessagePackSerializerOptions CreateDefaultMessagePackSerializerOptions() =>
631+
MessagePackSerializerOptions
632+
.Standard
633+
.WithResolver(SignalRResolver.Instance)
634+
.WithSecurity(MessagePackSecurity.UntrustedData);
664635

665636
internal class SignalRResolver : IFormatterResolver
666637
{
667638
public static readonly IFormatterResolver Instance = new SignalRResolver();
668639

669-
public static readonly IList<IFormatterResolver> Resolvers = new IFormatterResolver[]
640+
public static readonly IReadOnlyList<IFormatterResolver> Resolvers = new IFormatterResolver[]
670641
{
671642
DynamicEnumAsStringResolver.Instance,
672643
ContractlessStandardResolver.Instance,

src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading.Tasks;
1515
using MessagePack;
1616
using MessagePack.Formatters;
17+
using MessagePack.Resolvers;
1718
using Microsoft.AspNetCore.Authorization;
1819
using Microsoft.AspNetCore.Connections;
1920
using Microsoft.AspNetCore.Http;
@@ -2371,7 +2372,7 @@ public async Task HubOptionsCanUseCustomMessagePackSettings()
23712372
services.AddSignalR()
23722373
.AddMessagePackProtocol(options =>
23732374
{
2374-
options.FormatterResolvers.Insert(0, new CustomFormatter());
2375+
options.SerializerOptions = MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new CustomFormatter(), options.SerializerOptions.Resolver));
23752376
});
23762377
}, LoggerFactory);
23772378

0 commit comments

Comments
 (0)