/// <summary>
 /// Remove subscriptions
 /// </summary>
 public void UnSubscribe(string queueName, DispatcherQueueCallback callback)
 {
     if (this.m_watchers != null && this.m_watchers.TryGetValue(queueName, out var queueWatcher))
     {
         queueWatcher.Remove(callback);
     }
 }
        /// <summary>
        /// Stop the current service
        /// </summary>
        public bool Stop()
        {
            this.Stopping?.Invoke(this, EventArgs.Empty);

            ApplicationServiceContext.Current.GetService <IDispatcherQueueManagerService>().UnSubscribe(this.m_configuration.Gs1QueueName, this.m_handler);
            this.m_handler = null;

            this.Stopped?.Invoke(this, EventArgs.Empty);
            return(true);
        }
        /// <summary>
        /// Subscribe to the queue
        /// </summary>
        public void SubscribeTo(string queueName, DispatcherQueueCallback callback)
        {
            if (!this.m_watchers.TryGetValue(queueName, out var dispatcher))
            {
                dispatcher = new List <DispatcherQueueCallback>();
                this.m_watchers.TryAdd(queueName, dispatcher);
            }

            dispatcher.Add(callback);
        }
        /// <summary>
        /// Start the daemon service
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            this.Starting?.Invoke(this, EventArgs.Empty);

            // Create handler
            this.m_handler = (e) =>
            {
                do
                {
                    object body = null;
                    try
                    {
                        var dq = ApplicationServiceContext.Current.GetService <IDispatcherQueueManagerService>().Dequeue(this.m_configuration.Gs1QueueName);
                        if (dq == null)
                        {
                            break;
                        }
                        body = dq.Body;
                        this.SendQueueMessage(dq);
                    }
                    catch (Exception ex)
                    {
                        this.m_tracer.TraceError(">>>> !!ALERT!! >>>> Error sending message to GS1 broker. Message will be placed in dead-letter queue");
                        this.m_tracer.TraceError(ex.ToString());
                        ApplicationServiceContext.Current.GetService <IDispatcherQueueManagerService>().Enqueue($"{this.m_configuration.Gs1QueueName}.dead", body);
                    }
                } while (true);
            };

            // Queue Handler
            ApplicationServiceContext.Current.Started += (o, e) =>
            {
                var queueService = ApplicationServiceContext.Current.GetService <IDispatcherQueueManagerService>();
                queueService.Open(this.m_configuration.Gs1QueueName);
                queueService.SubscribeTo(this.m_configuration.Gs1QueueName, this.m_handler);
                queueService.Open($"{this.m_configuration.Gs1QueueName}.dead");
            };

            this.Started?.Invoke(this, EventArgs.Empty);
            return(true);
        }