static async Task <BrokeredMessage> GenerateBrokeredMessageWithBlobKeyPropertyAsync( Stream stream, IOrchestrationServiceBlobStore orchestrationServiceBlobStore, OrchestrationInstance instance, ServiceBusMessageSettings messageSettings, DateTime messageFireTime, string compressionType) { if (stream.Length > messageSettings.MessageMaxSizeInBytes) { throw new ArgumentException( $"The serialized message size {stream.Length} is larger than the supported external storage blob size {messageSettings.MessageMaxSizeInBytes}.", "stream"); } if (orchestrationServiceBlobStore == null) { throw new ArgumentException( "Please provide an implementation of IOrchestrationServiceBlobStore for external storage.", "orchestrationServiceBlobStore"); } // save the compressed stream using external storage when it is larger // than the supported message size limit. // the stream is stored using the generated key, which is saved in the message property. string blobKey = orchestrationServiceBlobStore.BuildMessageBlobKey(instance, messageFireTime); TraceHelper.TraceInstance( TraceEventType.Information, "GenerateBrokeredMessageWithBlobKeyProperty-SaveToBlob", instance, () => $"Saving the message stream in blob storage using key {blobKey}."); await orchestrationServiceBlobStore.SaveStreamAsync(blobKey, stream); BrokeredMessage brokeredMessage = new BrokeredMessage(); brokeredMessage.Properties[ServiceBusConstants.MessageBlobKey] = blobKey; brokeredMessage.Properties[FrameworkConstants.CompressionTypePropertyName] = compressionType; return(brokeredMessage); }
public static async Task <BrokeredMessage> GetBrokeredMessageFromObjectAsync( object serializableObject, CompressionSettings compressionSettings, ServiceBusMessageSettings messageSettings, OrchestrationInstance instance, string messageType, IOrchestrationServiceBlobStore orchestrationServiceBlobStore, DateTime messageFireTime) { if (serializableObject == null) { throw new ArgumentNullException(nameof(serializableObject)); } if (compressionSettings.Style == CompressionStyle.Legacy) { return(new BrokeredMessage(serializableObject) { SessionId = instance?.InstanceId }); } if (messageSettings == null) { messageSettings = new ServiceBusMessageSettings(); } bool disposeStream = true; var rawStream = new MemoryStream(); Utils.WriteObjectToStream(rawStream, serializableObject); try { BrokeredMessage brokeredMessage = null; if (compressionSettings.Style == CompressionStyle.Always || (compressionSettings.Style == CompressionStyle.Threshold && rawStream.Length > compressionSettings.ThresholdInBytes)) { Stream compressedStream = Utils.GetCompressedStream(rawStream); var rawLen = rawStream.Length; TraceHelper.TraceInstance( TraceEventType.Information, "GetBrokeredMessageFromObject-CompressionStats", instance, () => "Compression stats for " + (messageType ?? string.Empty) + " : " + brokeredMessage?.MessageId + ", uncompressed " + rawLen + " -> compressed " + compressedStream.Length); if (compressedStream.Length < messageSettings.MessageOverflowThresholdInBytes) { brokeredMessage = GenerateBrokeredMessageWithCompressionTypeProperty(compressedStream, FrameworkConstants.CompressionTypeGzipPropertyValue); } else { brokeredMessage = await GenerateBrokeredMessageWithBlobKeyPropertyAsync(compressedStream, orchestrationServiceBlobStore, instance, messageSettings, messageFireTime, FrameworkConstants.CompressionTypeGzipPropertyValue); } } else { if (rawStream.Length < messageSettings.MessageOverflowThresholdInBytes) { brokeredMessage = GenerateBrokeredMessageWithCompressionTypeProperty(rawStream, FrameworkConstants.CompressionTypeNonePropertyValue); disposeStream = false; } else { brokeredMessage = await GenerateBrokeredMessageWithBlobKeyPropertyAsync(rawStream, orchestrationServiceBlobStore, instance, messageSettings, messageFireTime, FrameworkConstants.CompressionTypeNonePropertyValue); } } brokeredMessage.SessionId = instance?.InstanceId; // TODO : Test more if this helps, initial tests shows not change in performance // brokeredMessage.ViaPartitionKey = instance?.InstanceId; return(brokeredMessage); } finally { if (disposeStream) { rawStream.Dispose(); } } }