internal HostOperation(
            OperationConfiguration <TRequest, TResponse> operation,
            HostOperationSettings settings)
        {
            ServiceName  = settings.ServiceName;
            ExchangeName = settings.ExchangeName;
            Serializer   = settings.Serializer;
            Logger       = settings.Logger;

            _operation  = operation;
            _connection = settings.Connection;
            _models     = new Dictionary <string, HostModel>();

            CreateQueue();

            for (var i = 0; i < NumberOfConsumers; i++)
            {
                var model = _connection.CreateModel();

                var consumer    = new AsyncEventingBasicConsumer(model);
                var consumerTag = model.BasicConsume(QueueName, true, consumer);
                consumer.Received += OnMessageReceived;

                _models.Add(consumerTag, new HostModel(model, consumerTag));
            }
        }
Exemple #2
0
        internal HostOperation(
            OperationConfiguration <TRequest, TResponse> operation,
            HostOperationSettings settings)
        {
            ServiceName  = settings.ServiceName;
            ExchangeName = settings.ExchangeName;
            Serializer   = settings.Serializer;
            Logger       = settings.Logger;

            _operation  = operation;
            _connection = settings.Connection;
            _model      = _connection.CreateModel();

            var queueName = QueueName;

            _model.QueueDeclare(queueName, true, false, false);
            _model.QueueBind(queueName, ExchangeName, operation.OperationName);

            var consumer = new EventingBasicConsumer(_model);

            _model.BasicConsume(queueName, false, consumer);
            consumer.Received += OnMessageReceived;
        }
Exemple #3
0
        public async Task StartAsync()
        {
            Logger.Log(LogLevel.Trace, "Starting CQRS host");

            try
            {
                if (_lock == null || _disposed)
                {
                    throw new ObjectDisposedException(nameof(CQRSHost));
                }

                await _lock.WaitAsync();

                if (_isStarted)
                {
                    throw new Exception("Host already started.");
                }

                // Dispose the previous connection and operations (if any)
                DisposeOperations();
                DisposeConnection();

                // Create a new RabbitMQ connection
                _connection = _config.CreateConnection();

                // Create the service exchange
                using (var model = _connection.CreateModel())
                {
                    model.ExchangeDeclare(ExchangeName, "direct", true, false);
                }

                // Create a model, queue, bindings and consumer for each operation
                var hostOperationGenericType = typeof(HostOperation <,>);

                var hostOperationSettings = new HostOperationSettings()
                {
                    Connection   = _connection,
                    Serializer   = Serializer,
                    Logger       = Logger,
                    ServiceName  = ServiceName,
                    ExchangeName = ExchangeName
                };

                foreach (var operation in _config.Operations)
                {
                    Type[] hostOperationTypeArgs =
                    {
                        operation.RequestType,
                        operation.ResponseType
                    };

                    var hostOperationType = hostOperationGenericType.MakeGenericType(hostOperationTypeArgs);

                    object[] hostOperationParams =
                    {
                        operation,
                        hostOperationSettings
                    };

                    var hostOperation = (IDisposable)Activator.CreateInstance(
                        hostOperationType,
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        hostOperationParams,
                        CultureInfo.InvariantCulture);

                    _operations.Add(hostOperation);
                }

                Logger.Log(LogLevel.Trace, "Started CQRS host");

                _isStarted = true;
            }
            finally
            {
                _lock?.Release();
            }
        }