/// <summary>
        /// Add the kafka listener to the container to enable consuming topics with your handlers.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="kafkaConfig"></param>
        /// <returns></returns>
        public static IDependencyRegistrationContext AddKafkaSender(this IDependencyRegistrationContext context, ProducerConfig kafkaConfig)
        {
            context.RegisterSingleton(c => new Producer <byte[], byte[]>(kafkaConfig), typeof(Producer <byte[], byte[]>));
            context.RegisterTransient(c => new KafkaMessageSender(c.Resolve <Producer <byte[], byte[]> >(), c.Resolve <IMessageSerializer>()));

            return(context);
        }
        /// <summary>
        /// Add the kafka listener to the container to enable consuming topics with your handlers.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="kafkaConfig"></param>
        /// <param name="consumeFromOffset">A dictionary with indexes for topics from which to start (does not need to be complete, topics without explicit indexes are restarted from the listener group index).</param>
        /// <returns></returns>
        public static IDependencyRegistrationContext AddKafkaListener(this IDependencyRegistrationContext context, ConsumerConfig kafkaConfig, string listenerGroupId, Dictionary <string, long> consumeFromOffset)
        {
            context.RegisterSingleton(c => new Consumer <byte[], byte[]>(kafkaConfig), typeof(Consumer <byte[], byte[]>));
            context.RegisterTransient(c => new KafkaMessageListener(c.Resolve <Consumer <byte[], byte[]> >(), listenerGroupId, consumeFromOffset, c.Resolve <IMessageSerializer>()));

            return(context);
        }
Beispiel #3
0
        /// <summary>
        /// Tell legion to scan for message handlers that are marked with the <see cref="MessageHandlerAttribute"/>.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="assembliesToScan">The assemblies that should be scanned for message handlers (if null only entry assembly is scanned).</param>
        /// <returns></returns>
        public static IDependencyRegistrationContext ScanForMessageHandlers(
            this IDependencyRegistrationContext context,
            IEnumerable <Assembly> assembliesToScan = null)
        {
            if (assembliesToScan == null)
            {
                assembliesToScan = new[] { Assembly.GetEntryAssembly() }.Distinct();
            }

            context.RegisterTransient(
                c => new MessageDispatcher(c.Resolve <IMessageListener>(), c.Resolve <IMessageHandlerRegistry>()),
                typeof(IMessageDispatcher));

            var messageHandlerRegistry = new MessageHandlerRegistry();

            context.RegisterSingleton(c =>
            {
                messageHandlerRegistry.ScanForMessageHandlerTypes(assembliesToScan, t => context.RegisterTransient(t, typeof(IMessageHandler), t));
                var resolutionContext = c.ResolveForLater();
                messageHandlerRegistry.ConnectContainerResolution(t => (IMessageHandler)resolutionContext.Resolve(t));
                return(messageHandlerRegistry);
            });

            return(context);
        }
Beispiel #4
0
        /// <summary>
        /// Use a simple in memory storage for transmitting messages.
        /// Good for testing.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IDependencyRegistrationContext UseInMemoryStorage(this IDependencyRegistrationContext context)
        {
            context.RegisterSingleton(c => new InMemoryMessageStore(), typeof(InMemoryMessageStore));
            context.RegisterTransient(c => new InMemoryMessageSender(c.Resolve <InMemoryMessageStore>()), typeof(IMessageSender));
            context.RegisterTransient(c => new InMemoryMessageListener(c.Resolve <InMemoryMessageStore>()), typeof(IMessageListener));

            return(context);
        }
Beispiel #5
0
        /// <summary>
        /// Tell legion to scan for message types that are marked with the <see cref="MessageAttribute"/>.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="assembliesToScan">The assemblies that should be scanned for messages (if null only entry assembly is scanned).</param>
        /// <returns></returns>
        public static IDependencyRegistrationContext ScanForMessages(
            this IDependencyRegistrationContext context,
            IEnumerable <Assembly> assembliesToScan = null)
        {
            if (assembliesToScan == null)
            {
                assembliesToScan = new[] { Assembly.GetEntryAssembly() }.Distinct();
            }

            context.RegisterSingleton(c => new MessageTypeRegistry(assembliesToScan), typeof(IMessageTypeRegistry));

            return(context);
        }