public static async Task <StateManagerLease> GetOrCreateAsync(
            IReliableStateManager StateManager,
            IReliableDictionary <string, string> StateDictionary,
            string EntryName,
            string partitionId)
        {
            using (ITransaction tx = StateManager.CreateTransaction())
            {
                StateManagerLease lease;
                // if something has been saved before load it
                ConditionalResult <string> cResults = await StateDictionary.TryGetValueAsync(tx, EntryName);

                if (cResults.HasValue)
                {
                    lease = FromJsonString(cResults.Value);
                    lease.m_StateDictionary = StateDictionary;
                    lease.m_StateManager    = StateManager;
                }
                else
                {
                    // if not create new
                    lease = new StateManagerLease(StateManager, StateDictionary, EntryName, partitionId);
                }
                await tx.CommitAsync();

                return(lease);
            }
        }
 public static async Task<StateManagerLease> GetOrCreateAsync(
     IReliableStateManager StateManager,
     IReliableDictionary<string, string> StateDictionary,
     string EntryName,
     string partitionId)
 {
     using (ITransaction tx = StateManager.CreateTransaction())
     {
         StateManagerLease lease;
         // if something has been saved before load it
         ConditionalResult<string> cResults = await StateDictionary.TryGetValueAsync(tx, EntryName);
         if (cResults.HasValue)
         {
             lease = FromJsonString(cResults.Value);
             lease.m_EntryName = EntryName;
             lease.m_StateDictionary = StateDictionary;
             lease.m_StateManager = StateManager;
         }
         else
         {
             // if not create new
             lease = new StateManagerLease(StateManager, StateDictionary, EntryName, partitionId);
         }
         await tx.CommitAsync();
         return lease;
     }
 }
        /// <summary>
        /// Checkpoints the asynchronous.
        /// </summary>
        /// <param name="lease">The lease.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="sequenceNumber">The sequence number.</param>
        /// <returns>The Task</returns>
        public async Task CheckpointAsync(Lease lease, string offset, long sequenceNumber)
        {
            StateManagerLease stateManagerLease = lease as StateManagerLease;

            stateManagerLease.Offset         = offset;
            stateManagerLease.SequenceNumber = sequenceNumber;
            await stateManagerLease.SaveAsync();
        }
        public async Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            this.m_MessagingFactory = MessagingFactory.CreateFromConnectionString(this.EventHubConnectionString);
            this.m_EventHubClient   = this.m_MessagingFactory.CreateEventHubClient(this.EventHubName);
            this.m_ConsumerGroup    = !string.IsNullOrEmpty(this.EventHubConsumerGroupName)
                ? this.m_EventHubClient.GetConsumerGroup(this.EventHubConsumerGroupName)
                : this.m_EventHubClient.GetDefaultConsumerGroup();


            // slice the pie according to distribution
            // this partition can get one or more assigned Event Hub Partition ids
            string[] EventHubPartitionIds  = this.m_EventHubClient.GetRuntimeInformation().PartitionIds;
            string[] assignedPartitionsIds = await this.ResolveEventHubPartitions(EventHubPartitionIds);

            this.m_EventProcessorFactory = new EventProcessorFactory(this.Handler, this.EventHubName, this.Namespace, this.EventHubConsumerGroupName);
            CheckPointManager checkPointManager = new CheckPointManager();


            this.m_TraceWriter.TraceMessage(
                string.Format(
                    "Event Hub Listener for {0} on {1} using mode:{2} handling:{3}/{4} event hub partitions",
                    this.EventHubName,
                    this.Namespace,
                    this.ListenerMode,
                    assignedPartitionsIds.Count(),
                    EventHubPartitionIds.Count()));


            foreach (string pid in assignedPartitionsIds)
            {
                StateManagerLease lease =
                    await
                    StateManagerLease.GetOrCreateAsync(
                        this.StateManager,
                        this.StateDictionary,
                        this.m_Namespace,
                        this.EventHubConsumerGroupName,
                        this.EventHubName,
                        pid);


                await this.m_ConsumerGroup.RegisterProcessorFactoryAsync(
                    lease,
                    checkPointManager,
                    this.m_EventProcessorFactory);
            }


            return(string.Concat(this.EventHubName, " @ ", this.Namespace));
        }