Esempio n. 1
0
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            try
            {
                //IEnumerable<QueueMessage> qm = await _receiver.ReceiveMessagesAsync(100);

                await _publisher.PutMessagesAsync(new[] { QueueMessage.FromText("content at " + DateTime.UtcNow) });

                //qm = await _receiver.ReceiveMessagesAsync(100);

                //separate writes
                await _blobs.WriteTextAsync("one", "test text 1");

                await _blobs.WriteTextAsync("two", "test text 2");

                //with transaction object
                using (ITransaction tx = await _blobs.OpenTransactionAsync())
                {
                    await _blobs.WriteTextAsync("three", "test text 1");

                    await _blobs.WriteTextAsync("four", "test text 2");

                    await tx.CommitAsync();
                }

                IEnumerable <BlobId> keys = await _blobs.ListAsync(null);

                string textBack = await _blobs.ReadTextAsync("one");

                textBack = await _blobs.ReadTextAsync("two");
            }
            catch (Exception ex)
            {
                throw;
            }

            IReliableDictionary <string, long> myDictionary = await StateManager.GetOrAddAsync <IReliableDictionary <string, long> >("myDictionary");

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (SFT tx = this.StateManager.CreateTransaction())
                {
                    Microsoft.ServiceFabric.Data.ConditionalValue <long> result = await myDictionary.TryGetValueAsync(tx, "Counter");

                    ServiceEventSource.Current.ServiceMessage(this.Context, "Current Counter Value: {0}",
                                                              result.HasValue ? result.Value.ToString() : "Value does not exist.");

                    await myDictionary.AddOrUpdateAsync(tx, "Counter", 0, (key, value) => ++ value);

                    // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
                    // discarded, and nothing is saved to the secondary replicas.
                    await tx.CommitAsync();
                }

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
            }
        }
        private async Task <IEnumerable <T> > GetResults <T>(Microsoft.ServiceFabric.Data.ITransaction tx, IReliableDictionary <Guid, T> myDictionary)
        {
            var results    = new List <T>();
            var enumerable = await myDictionary.CreateEnumerableAsync(tx);

            var enumerator = enumerable.GetAsyncEnumerator();
            var ct         = new CancellationToken();

            while (await enumerator.MoveNextAsync(ct))
            {
                results.Add(enumerator.Current.Value);
            }
            return(results);
        }
 public ReliableStateTransactionContext(IReliableStateTransaction reliableTransaction)
 {
     ReliableTransaction = reliableTransaction;
 }