/// <summary>
        /// Creates the Subscription with EventStore.
        /// </summary>
        /// <param name="streamName">The name of the EventStore Stream we are subscribing to.</param>
        /// <param name="eventStoreStreamSequenceToken"></param>
        /// <returns>An EventStoreSubscription or EventStorePersistentSubscription.</returns>
        private object CreateEventStoreSubscription(string streamName, EventStoreStreamSequenceToken eventStoreStreamSequenceToken)
        {
            object subscription = null;

            if (eventStoreStreamSequenceToken == null || eventStoreStreamSequenceToken.EventNumber == int.MinValue)
            {
                var subscribeTask = m_EventStoreConnection.SubscribeToStreamAsync(streamName, true, EventAppeared,
                                                                                  SubscriptionDropped);
                Task.WaitAll(subscribeTask);
                if (subscribeTask.IsFaulted)
                {
                    throw subscribeTask.Exception;
                }

                subscription = subscribeTask.Result;
            }
            else
            {
                // If we have been provided with a StreamSequenceToken, we can call SubscribeFrom to get previous messages.
                subscription = m_EventStoreConnection.SubscribeToStreamFrom(streamName,
                                                                            eventStoreStreamSequenceToken.EventNumber, new CatchUpSubscriptionSettings(100, 20, false, true), EventAppeared);
            }

            m_Logger.Info($"Subscribed to Stream {streamName}");
            return(subscription);
        }
 /// <summary>
 /// Notify the AdapterRecevier that it should subscribe to an EventStore Stream.
 /// </summary>
 /// <param name="streamNamespace"></param>
 /// <param name="eventStoreStreamSequenceToken"></param>
 public void SubscribeTo(string streamNamespace, EventStoreStreamSequenceToken eventStoreStreamSequenceToken)
 {
     m_CachedSubscriptions.AddOrUpdate(streamNamespace, name => CreateEventStoreSubscription(name, eventStoreStreamSequenceToken),
                                       (streamName, stream) => stream); // Update is a NO-OP.
 }
        private void EnqueueEvent(string streamId, ResolvedEvent resolvedEvent)
        {
            var sequenceToken = new EventStoreStreamSequenceToken(resolvedEvent.Event.EventNumber);

            m_MessagesAwaitingDelivery.Enqueue(new EventStoreBatchContainer(Guid.Empty, streamId, sequenceToken, resolvedEvent));
        }