Exemplo n.º 1
0
 /// <summary>
 /// 添加监听
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="onMessageRecieved"></param>
 /// <returns></returns>
 public static IKafkaConsumerBuilder AddListener(this IKafkaConsumerBuilder builder, Action <RecieveResult> onMessageRecieved)
 {
     return(builder.AddListener((_, result) =>
     {
         onMessageRecieved?.Invoke(result);
     }));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 添加自定义监听
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="listenerType"></param>
        /// <returns></returns>
        public static IKafkaConsumerBuilder AddListener(this IKafkaConsumerBuilder builder, Type listenerType)
        {
            if (!typeof(IKafkaConsumerListener).IsAssignableFrom(listenerType) || !listenerType.IsClass || listenerType.IsAbstract)
            {
                throw new ArgumentException($"the listener type must be implement {nameof(IKafkaConsumerListener)} and none abstract class", nameof(listenerType));
            }

            builder.Services.AddTransient(listenerType);
            return(builder.AddListener((serviceProvider, result) =>
            {
                var listenner = serviceProvider.GetService(listenerType) as IKafkaConsumerListener;
                listenner.ConsumeAsync(result).Wait();
            }));
        }
Exemplo n.º 3
0
 /// <summary>
 /// 添加自定义监听
 /// </summary>
 /// <param name="builder"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static IKafkaConsumerBuilder AddListener <T>(this IKafkaConsumerBuilder builder) where T : class, IKafkaConsumerListener
 {
     return(builder.AddListener(typeof(T)));
 }