Example #1
0
        public void EnsureInitialization(IConnection connection, IMessageParserRegistry messageParserRegistry)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (messageParserRegistry is null)
            {
                throw new ArgumentNullException(nameof(messageParserRegistry));
            }

            if (!_initialized)
            {
                var messageParser = messageParserRegistry.Retrieve(_commandHandlerContext.ParserType);
                if (!messageParser.CanParse(_commandHandlerContext.ParameterType))
                {
                    throw new ServiceBuilderException($"The {_commandHandlerContext.ParserType} can not parse messages of type {_commandHandlerContext.ParameterType}.");
                }

                _consumer = connection.CreateConsumer(_commandHandlerContext.Topic);
                _consumer.RegisterConsumer(_commandHandlerContext.Queue, (sender, args) =>
                {
                    //TODO - Add manual acknowledge functionality.
                    var body    = args.Body;
                    var message = messageParser.Parse(body);
                    _commandHandlerContext.TargetMethod.Invoke(_instance, new object[] { message });
                });
                _initialized = true;
            }
        }
 public TopicSubscriptionActivator(IConnection connection, IMessageParserRegistry messageParserRegistry)
 {
     _connection            = connection ?? throw new ArgumentNullException(nameof(connection));
     _messageParserRegistry = messageParserRegistry ?? throw new ArgumentNullException(nameof(messageParserRegistry));
 }