/// <summary> /// Receive messages from message queue. /// This method will register the given function to consume queue. /// </summary> /// <param name="name">Name of queue or whatever using to describing work/job/event. Same as Send method's name.</param> /// <param name="func">Receiver function.</param> /// <returns>Returns true if receiver method registered successfully, otherwise returns false.</returns> /// <exception cref="ArgumentNullException">Throws when name or func is null.</exception> public bool Receive(string name, Func <string, bool> func) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } if (func == null) { throw new ArgumentNullException(nameof(func)); } // Get new channel and connect receiver to it. _channelFactory.Channel(channel => { // Prepare channel to consume messages and retry. var queueName = name; // To consume. var exchangeName = PrepareChannel(channel, queueName); // To retry. PrepareChannelForRetry(channel, queueName, exchangeName); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); // Create new consumer. var consumer = _eventingBasicConsumerImpl.GetEventingBasicConsumer(channel); // Register to job received event. consumer.Received += (model, ea) => { this.Invoke(func, ea, channel, queueName); }; // Start consuming. channel.BasicConsume(queueName, autoAck: false, consumer: consumer); }); return(true); }