Ejemplo n.º 1
0
        public RequestResult TryToExecute(string type, string message, string callbackQueueName)
        {
            var connection = _connectionFactory.CreateConnection();

            try
            {
                var channel = connection.CreateModel();

                channel.ExchangeDeclare("amq.direct", ExchangeType.Direct, true);
                channel.QueueDeclare(callbackQueueName, false, false, true, null);
                channel.QueueBind(callbackQueueName, "amq.direct", callbackQueueName, null);

                _senderProcessor.SendMessage(type, message);

                var subscription = _subsctiptionFactory.CreateSubscription(channel, callbackQueueName, false);
                var result       = subscription.Next(_timeToWait, out var basicDeliveryEventArgs);
                if (result)
                {
                    var messageContent = Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
                    subscription.Ack(basicDeliveryEventArgs);
                    connection.Close();
                    return(new RequestResult {
                        Result = true, Value = messageContent
                    });
                }
                else
                {
                    connection.Close();
                    return(new RequestResult {
                        Result = false
                    });
                }
            }
            catch (Exception)
            {
                connection.Close();
                throw;
            }
        }
Ejemplo n.º 2
0
        public void OnUserSubscribing(Hub <TClient> hub, int subscriptionId, TSubscribeParameters parameters)
        {
            var newSubscription = _subscriptionFactory.CreateSubscription(hub.Clients.Caller, parameters);

            var existingSubscriptions = GetClientSubscriptions(hub.Context.ConnectionId);

            lock (existingSubscriptions)
            {
                if (existingSubscriptions.Subscriptions.ContainsKey(subscriptionId))
                {
                    throw new InvalidOperationException($"Subscription already exists for subscriptionId '{subscriptionId}'");
                }

                existingSubscriptions.Subscriptions.Add(subscriptionId, newSubscription);

                if (existingSubscriptions.Deleted)
                {
                    existingSubscriptions.Deleted = false;

                    lock (_subscriptionsByConnectionId)
                        _subscriptionsByConnectionId.Add(hub.Context.ConnectionId, existingSubscriptions);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Starts any new subscriptions
 /// </summary>
 /// <param name="activeSubscriptions"></param>
 private void AddNewSubscriptions(IEnumerable <Subscription> activeSubscriptions)
 {
     activeSubscriptions.Where(s => !_subscriptions.ContainsKey(s.ID))
     .ForEach(s => _subscriptions.Add(s.ID, _subscriptionFactory.CreateSubscription(s)));
 }