public static async Task ForEach <T>(
            [NotNull] this Microsoft.ServiceFabric.Data.IAsyncEnumerable <T> enumerable,
            CancellationToken token,
            [NotNull][InstantHandle] Action <T> block)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }

            if (block == null)
            {
                throw new ArgumentNullException(nameof(block));
            }

            using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <T> enumerator = enumerable.GetAsyncEnumerator())
            {
                while (await enumerator.MoveNextAsync(token))
                {
                    block(enumerator.Current);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <Product.Model.Product> > GetAllProducts()
        {
            IReliableDictionary <Guid, Product.Model.Product> products = await
                                                                         _stateManager.GetOrAddAsync <IReliableDictionary <Guid, Product.Model.Product> >("products");

            var result = new List <Product.Model.Product>();

            using (ITransaction tx = _stateManager.CreateTransaction())
            {
                Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <Guid, Product.Model.Product> > allProducts = await
                                                                                                                          products.CreateEnumerableAsync(tx, EnumerationMode.Unordered);

                using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <Guid, Product.Model.Product> > enumerator = allProducts.GetAsyncEnumerator())
                {
                    while (await enumerator.MoveNextAsync(CancellationToken.None))
                    {
                        KeyValuePair <Guid, Product.Model.Product> current = enumerator.Current;
                        result.Add(current.Value);
                    }
                }
            }

            return(result);
        }
        public static async Task ForEach <TKey, TValue>(
            [NotNull] this Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <TKey, TValue> > enumerable,
            CancellationToken token,
            [NotNull][InstantHandle] Func <TKey, TValue, Task> block)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }

            if (block == null)
            {
                throw new ArgumentNullException(nameof(block));
            }

            using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <TKey, TValue> > enumerator = enumerable.GetAsyncEnumerator())
            {
                while (await enumerator.MoveNextAsync(token))
                {
                    KeyValuePair <TKey, TValue> pair = enumerator.Current;
                    await block(pair.Key, pair.Value);
                }
            }
        }
Exemplo n.º 4
0
        //Generic query method returning a IList<KeyValuePair<Guid, T>>
        public static async Task <IList <KeyValuePair <Guid, T> > > QueryReliableDictionaryKeyValuePairList <T>(Microsoft.ServiceFabric.Data.IReliableStateManager stateManager,
                                                                                                                string reliableDictionaryName,
                                                                                                                Func <T, bool> filter)
        {
            var result = new List <KeyValuePair <Guid, T> >();

            IReliableDictionary <Guid, T> reliableDictionary =
                await stateManager.GetOrAddAsync <IReliableDictionary <Guid, T> >(reliableDictionaryName);

            using (var tx = stateManager.CreateTransaction())
            {
                Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <Guid, T> > asyncEnumerable =
                    await reliableDictionary.CreateEnumerableAsync(tx);

                using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <Guid, T> > asyncEnumerator = asyncEnumerable.GetAsyncEnumerator())
                {
                    while (await asyncEnumerator.MoveNextAsync(CancellationToken.None))
                    {
                        if (filter(asyncEnumerator.Current.Value))
                        {
                            result.Add(asyncEnumerator.Current);
                        }
                    }
                }
            }
            return(result);
        }