diff --git a/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs b/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs index c691b19183..d05289171d 100644 --- a/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs +++ b/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Net; using System.Runtime.InteropServices; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Bot.Connector.Streaming.Payloads; @@ -19,6 +18,8 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Net.Http.Headers; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; namespace Microsoft.Bot.Connector.Streaming.Session { @@ -42,6 +43,13 @@ internal class StreamingSession private readonly object _receiveSync = new object(); + static StreamingSession() + { + var tmpSetting = SerializationSettings.DefaultSerializationSettings; + tmpSetting.NullValueHandling = NullValueHandling.Ignore; + Serializer = JsonSerializer.Create(tmpSetting); + } + public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogger logger, CancellationToken connectionCancellationToken = default) { _receiver = receiver ?? throw new ArgumentNullException(nameof(receiver)); @@ -52,6 +60,8 @@ public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogge _connectionCancellationToken = connectionCancellationToken; } + private static JsonSerializer Serializer { get; } + public async Task SendRequestAsync(StreamingRequest request, CancellationToken cancellationToken) { if (request == null) @@ -461,10 +471,16 @@ private static T DeserializeTo(ReadOnlySequence payload) mainPayload = payload.Slice(_utf8Bom.Length); } - var reader = new Utf8JsonReader(mainPayload); - return System.Text.Json.JsonSerializer.Deserialize( - ref reader, - new JsonSerializerOptions() { IgnoreNullValues = true, PropertyNameCaseInsensitive = true }); + using (var ms = new MemoryStream(mainPayload.ToArray())) + { + using (var sr = new StreamReader(ms)) + { + using (var jsonReader = new JsonTextReader(sr)) + { + return Serializer.Deserialize(jsonReader); + } + } + } } private static void CreatePlaceholderStreams(Header header, List placeholders, List streamInfo)