/// <summary> /// Get the property value. /// </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(ITurnContext 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) { throw new MissingMemberException("Property not set and no default provided."); } var result = defaultValueFactory(); // save default value for any further calls await SetAsync(turnContext, result, cancellationToken).ConfigureAwait(false); return(result); } }
/// <summary> /// Delete the property. The semantics are intended to be lazy, note the use of LoadAsync at the start. /// </summary> /// <param name="turnContext">The turn context.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task DeleteAsync(ITurnContext turnContext, CancellationToken cancellationToken) { await _botState.LoadAsync(turnContext, false, cancellationToken).ConfigureAwait(false); await _botState.DeletePropertyValueAsync(turnContext, Name, cancellationToken).ConfigureAwait(false); }