Пример #1
0
        /// <summary>
        /// If it has changed, writes to storage the state object that is cached in the current context object for this turn.
        /// </summary>
        /// <param name="turnContext">The context object for this turn.</param>
        /// <param name="force">Optional. True to save state to storage whether or not there are changes.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        public async Task SaveChangesAsync(TurnContext turnContext, bool force = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var cachedState = turnContext.TurnState.Get <CachedBotState>(_contextServiceKey);

            if (force || (cachedState != null && cachedState.IsChanged()))
            {
                var key     = GetStorageKey(turnContext);
                var changes = new Dictionary <string, object>
                {
                    { key, cachedState.State },
                };
                await _storage.WriteAsync(changes).ConfigureAwait(false);

                cachedState.Hash = cachedState.ComputeHash(cachedState.State);
                return;
            }
        }
Пример #2
0
            /// <summary>
            /// Get the property value. The semantics are intended to be lazy, note the use of LoadAsync at the start.
            /// </summary>
            /// <param name="turnContext">The context object for this turn.</param>
            /// <param name="defaultValueFactory">Defines the default value. Invoked when no value been set for the requested state property.  If defaultValueFactory is defined as null, the MissingMemberException will be thrown if the underlying property is not set.</param>
            /// <param name="cancellationToken">The cancellation token.</param>
            /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
            public async Task <T> GetAsync(TurnContext turnContext, Func <T> defaultValueFactory, CancellationToken cancellationToken)
            {
                await _botState.LoadAsync(turnContext, false, cancellationToken).ConfigureAwait(false);

                try
                {
                    return(await _botState.GetPropertyValueAsync <T>(turnContext, Name, cancellationToken).ConfigureAwait(false));
                }
                catch (KeyNotFoundException)
                {
                    // ask for default value from factory
                    if (defaultValueFactory == null)
                    {
                        return(default(T));
                    }

                    var result = defaultValueFactory();

                    // save default value for any further calls
                    await SetAsync(turnContext, result, cancellationToken).ConfigureAwait(false);

                    return(result);
                }
            }
Пример #3
0
 public async Task SaveAllChangesAsync(TurnContext turnContext, bool force = false, CancellationToken cancellationToken = default(CancellationToken))
 {
     var tasks = this.BotStates.Select(bs => bs.SaveChangesAsync(turnContext, force, cancellationToken)).ToList();
     await Task.WhenAll(tasks).ConfigureAwait(false);
 }
Пример #4
0
            /// <summary>
            /// Set the property value. The semantics are intended to be lazy, note the use of LoadAsync at the start.
            /// </summary>
            /// <param name="turnContext">turn context.</param>
            /// <param name="value">value.</param>
            /// <param name="cancellationToken">The cancellation token.</param>
            /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
            public async Task SetAsync(TurnContext turnContext, T value, CancellationToken cancellationToken)
            {
                await _botState.LoadAsync(turnContext, false, cancellationToken).ConfigureAwait(false);

                await _botState.SetPropertyValueAsync(turnContext, Name, value, cancellationToken).ConfigureAwait(false);
            }
Пример #5
0
 /// <summary>
 /// When overridden in a derived class, gets the key to use when reading and writing state to and from storage.
 /// </summary>
 /// <param name="turnContext">The context object for this turn.</param>
 /// <returns>The storage key.</returns>
 protected abstract string GetStorageKey(TurnContext turnContext);
Пример #6
0
 protected virtual Task OnRepromptDialogAsync(TurnContext turnContext, DialogInstance instance, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.CompletedTask);
 }
Пример #7
0
 protected virtual Task OnEndDialogAsync(TurnContext context, DialogInstance instance, DialogReason reason, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.CompletedTask);
 }