/// <summary>
        /// Gets a value that has previously been stored in this context, loading data on-demand to avoid overhead for
        /// every task that may not have any data (i.e. do not pre-load as most would be empty operations).
        /// </summary>
        /// <param name="key">The key of the item to be loaded.</param>
        /// <typeparam name="T">The type of the value that has been stored.</typeparam>
        /// <returns>The stored value, or <c>default(T)</c> if it does not exist.</returns>
        public async Task <T> GetAsync <T>(string key)
        {
            if (!this._loaded)
            {
                var loadedData = await this._provider.LoadDataAsync(this._contextKey)
                                 ?? new BackgroundTaskContextData(this._contextKey);

                // If data has been modified before loading we need to merge those
                // changes in to the existing data
                if (this._modified)
                {
                    foreach (var modifiedKvp in this._data.Data)
                    {
                        loadedData.Data[modifiedKvp.Key] = modifiedKvp.Value;
                    }
                }

                this._data   = loadedData;
                this._loaded = true;
            }

            if (this._data.Data.TryGetValue(key, out var value))
            {
                return((T)value);
            }

            return(default);
Esempio n. 2
0
 /// <inheritdoc />
 /// <exception cref="NotImplementedException">Always thrown.</exception>
 public Task SaveDataAsync(BackgroundTaskContextData data)
 {
     throw new NotImplementedException("InMemoryBackgroundTaskContextProvider is read-only");
 }