Ejemplo n.º 1
0
        static Task <Stream> LoadMessageStreamAsync(BrokeredMessage message, IOrchestrationServiceBlobStore orchestrationServiceBlobStore)
        {
            object blobKeyObj = null;
            string blobKey    = string.Empty;

            if (message.Properties.TryGetValue(ServiceBusConstants.MessageBlobKey, out blobKeyObj))
            {
                blobKey = (string)blobKeyObj;
            }

            if (string.IsNullOrEmpty(blobKey))
            {
                // load the stream from the message directly if the blob key property is not set,
                // i.e., it is not stored externally
                return(Task.Run(() => message.GetBody <Stream>()));
            }

            // if the blob key is set in the message property,
            // load the stream message from the service bus message store.
            if (orchestrationServiceBlobStore == null)
            {
                throw new ArgumentException($"Failed to load compressed message from external storage with key: {blobKey}. Please provide an implementation of IServiceBusMessageStore for external storage.", nameof(orchestrationServiceBlobStore));
            }

            return(orchestrationServiceBlobStore.LoadStreamAsync(blobKey));
        }
        static Task <Stream> LoadMessageStreamAsync(Message message, IOrchestrationServiceBlobStore orchestrationServiceBlobStore)
        {
            string blobKey = string.Empty;

            if (message.UserProperties.TryGetValue(ServiceBusConstants.MessageBlobKey, out object blobKeyObj))
            {
                blobKey = (string)blobKeyObj;
            }

            if (string.IsNullOrWhiteSpace(blobKey))
            {
                // load the stream from the message directly if the blob key property is not set,
                // i.e., it is not stored externally
#if NETSTANDARD2_0
                return(Task.Run(() => new System.IO.MemoryStream(message.Body) as Stream));
#else
                return(Task.Run(() => message.GetBody <Stream>()));
#endif
            }

            // if the blob key is set in the message property,
            // load the stream message from the service bus message store.
            if (orchestrationServiceBlobStore == null)
            {
                throw new ArgumentException($"Failed to load compressed message from external storage with key: {blobKey}. Please provide an implementation of IServiceBusMessageStore for external storage.", nameof(orchestrationServiceBlobStore));
            }

            return(orchestrationServiceBlobStore.LoadStreamAsync(blobKey));
        }
        /// <summary>
        /// Convert a raw stream to an orchestration runtime state instance.
        /// </summary>
        /// <param name="rawSessionStream">The raw session stream to be deserialized</param>
        /// <param name="sessionId">The session Id</param>
        /// <param name="orchestrationServiceBlobStore">A blob store for external blob storage</param>
        /// <param name="dataConverter">>A data converter for serialization and deserialization</param>
        /// <returns></returns>
        public static async Task <OrchestrationRuntimeState> RawStreamToRuntimeState(Stream rawSessionStream, string sessionId, IOrchestrationServiceBlobStore orchestrationServiceBlobStore, DataConverter dataConverter)
        {
            bool isEmptySession;
            OrchestrationRuntimeState runtimeState;
            Stream sessionStream = await Utils.GetDecompressedStreamAsync(rawSessionStream);

            isEmptySession = sessionStream == null;
            long rawSessionStateSize = isEmptySession ? 0 : rawSessionStream.Length;
            long newSessionStateSize = isEmptySession ? 0 : sessionStream.Length;

            string blobKey;

            runtimeState = GetOrCreateInstanceState(sessionStream, sessionId, dataConverter, out blobKey);

            if (string.IsNullOrWhiteSpace(blobKey))
            {
                TraceHelper.TraceSession(
                    TraceEventType.Information,
                    "RuntimeStateStreamConverter-StreamToRuntimeStateSize",
                    sessionId,
                    $"Size of session state is {newSessionStateSize}, compressed {rawSessionStateSize}");
                return(runtimeState);
            }

            if (orchestrationServiceBlobStore == null)
            {
                throw new OrchestrationException(
                          $"Please provide an implementation of IOrchestrationServiceBlobStore for external storage to load the runtime state.");
            }

            TraceHelper.TraceSession(
                TraceEventType.Information,
                "RuntimeStateStreamConverter-StreamToRuntimeStateLoadFromStorage",
                sessionId,
                $"Loading the serialzied stream from external storage with blob key {blobKey}.");

            Stream externalStream = await orchestrationServiceBlobStore.LoadStreamAsync(blobKey);

            return(await RawStreamToRuntimeState(externalStream, sessionId, orchestrationServiceBlobStore, dataConverter));
        }