public override async Task WriteMessageAsync(Message message, Stream stream) { if (message == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); if (stream == null) throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); ThrowIfMismatchedMessageVersion(message); EventTraceActivity eventTraceActivity = null; if (TD.TextMessageEncodingStartIsEnabled()) { eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); TD.TextMessageEncodingStart(eventTraceActivity); } message.Properties.Encoder = this; XmlDictionaryWriter xmlWriter = TakeStreamedWriter(stream); if (_optimizeWriteForUTF8) { await message.WriteMessageAsync(xmlWriter); } else { xmlWriter.WriteStartDocument(); await message.WriteMessageAsync(xmlWriter); xmlWriter.WriteEndDocument(); } await xmlWriter.FlushAsync(); ReturnStreamedWriter(xmlWriter); if (TD.StreamedMessageWrittenByEncoderIsEnabled()) { TD.StreamedMessageWrittenByEncoder(eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message)); } if (MessageLogger.LogMessagesAtTransportLevel) MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); }