async static Task <Stream> CreateStreamForExternalStorageAsync(
            bool shouldCompress,
            IOrchestrationServiceBlobStore orchestrationServiceBlobStore,
            string sessionId,
            DataConverter dataConverter,
            Stream compressedState)
        {
            if (orchestrationServiceBlobStore == null)
            {
                throw new OrchestrationException(
                          "The compressed session is larger than supported. " +
                          "Please provide an implementation of IOrchestrationServiceBlobStore for external storage.");
            }

            // create a new orchestration session state with the external blob key
            string key = orchestrationServiceBlobStore.BuildSessionBlobKey(sessionId);

            TraceHelper.TraceSession(
                TraceEventType.Information,
                "RuntimeStateStreamConverter-SaveSessionToStorage",
                sessionId,
                $"Saving the serialized stream in external storage with key {key}.");

            // save the compressedState stream externally as a blob
            await orchestrationServiceBlobStore.SaveStreamAsync(key, compressedState);

            // create an OrchestrationSessionState instance to hold the blob key,
            // and then serialize the instance as a sream for the session state
            OrchestrationSessionState orchestrationSessionState = new OrchestrationSessionState(key);
            string serializedStateExternal = dataConverter.Serialize(orchestrationSessionState);

            long   streamSize;
            Stream compressedStateForSession = Utils.WriteStringToStream(
                serializedStateExternal,
                shouldCompress,
                out streamSize);

            return(compressedStateForSession);
        }
        /// <summary>
        /// Convert an OrchestrationRuntimeState instance to a serialized raw stream to be saved in session state.
        /// </summary>
        /// <param name="newOrchestrationRuntimeState">The new OrchestrationRuntimeState to be serialized</param>
        /// <param name="runtimeState">The current runtime state</param>
        /// <param name="dataConverter">A data converter for serialization and deserialization</param>
        /// <param name="shouldCompress">True if should compress when serialization</param>
        /// <param name="serviceBusSessionSettings">The service bus session settings</param>
        /// <param name="orchestrationServiceBlobStore">A blob store for external blob storage</param>
        /// <param name="sessionId">The session id</param>
        /// <returns>A serialized raw strem to be saved in session state</returns>
        public static async Task <Stream> OrchestrationRuntimeStateToRawStream(
            OrchestrationRuntimeState newOrchestrationRuntimeState,
            OrchestrationRuntimeState runtimeState,
            DataConverter dataConverter,
            bool shouldCompress,
            ISessionSettings serviceBusSessionSettings,
            IOrchestrationServiceBlobStore orchestrationServiceBlobStore,
            string sessionId)
        {
            OrchestrationSessionState orchestrationSessionState = new OrchestrationSessionState(newOrchestrationRuntimeState.Events);
            string serializedState = dataConverter.Serialize(orchestrationSessionState);

            long   originalStreamSize = 0;
            Stream compressedState    = Utils.WriteStringToStream(
                serializedState,
                shouldCompress,
                out originalStreamSize);

            runtimeState.Size           = originalStreamSize;
            runtimeState.CompressedSize = compressedState.Length;

            if (runtimeState.CompressedSize > serviceBusSessionSettings.SessionMaxSizeInBytes)
            {
                throw new OrchestrationException($"Session state size of {runtimeState.CompressedSize} exceeded the termination threshold of {serviceBusSessionSettings.SessionMaxSizeInBytes} bytes");
            }

            if (runtimeState.CompressedSize > serviceBusSessionSettings.SessionOverflowThresholdInBytes)
            {
                TraceHelper.TraceSession(
                    TraceEventType.Information,
                    "RuntimeStateStreamConverter-SessionStateThresholdExceeded",
                    sessionId,
                    $"Session state size of {runtimeState.CompressedSize} exceeded the termination threshold of {serviceBusSessionSettings.SessionOverflowThresholdInBytes} bytes." +
                    $"Creating an OrchestrationSessionState instance with key for exteranl storage.");
                return(await CreateStreamForExternalStorageAsync(shouldCompress, orchestrationServiceBlobStore, sessionId, dataConverter, compressedState));
            }

            return(compressedState);
        }