From b08577342dc39bc8e6051a66311bfc40f623fccc Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 6 Jun 2019 08:33:32 -0700 Subject: [PATCH] Lazy-init formatter resolvers in MessagePackHubProtocolOptions (#10929) --- ...lR.Protocols.MessagePack.netstandard2.0.cs | 2 +- .../src/MessagePackHubProtocolOptions.cs | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/SignalR/common/Protocols.MessagePack/ref/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.netstandard2.0.cs b/src/SignalR/common/Protocols.MessagePack/ref/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.netstandard2.0.cs index ec222f2318..70f7ce9d02 100644 --- a/src/SignalR/common/Protocols.MessagePack/ref/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.netstandard2.0.cs +++ b/src/SignalR/common/Protocols.MessagePack/ref/Microsoft.AspNetCore.SignalR.Protocols.MessagePack.netstandard2.0.cs @@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.SignalR public partial class MessagePackHubProtocolOptions { public MessagePackHubProtocolOptions() { } - public System.Collections.Generic.IList FormatterResolvers { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + public System.Collections.Generic.IList FormatterResolvers { get { throw null; } set { } } } } namespace Microsoft.AspNetCore.SignalR.Protocol diff --git a/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs b/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs index 95ddfe6a9b..85025b6444 100644 --- a/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs +++ b/src/SignalR/common/Protocols.MessagePack/src/MessagePackHubProtocolOptions.cs @@ -9,6 +9,25 @@ namespace Microsoft.AspNetCore.SignalR { public class MessagePackHubProtocolOptions { - public IList FormatterResolvers { get; set; } = MessagePackHubProtocol.CreateDefaultFormatterResolvers(); + private IList _formatterResolvers; + + public IList FormatterResolvers + { + get + { + if (_formatterResolvers == 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(); + } + + return _formatterResolvers; + } + set + { + _formatterResolvers = value; + } + } } }