internal static object ResolveValueSerializer(Type valueType, string specifiedAvroSchema)
        {
            if (typeof(Google.Protobuf.IMessage).IsAssignableFrom(valueType))
            {
                return(Activator.CreateInstance(typeof(ProtobufSerializer <>).MakeGenericType(valueType)));
            }

            var isSpecificRecord = typeof(ISpecificRecord).IsAssignableFrom(valueType);

            if (isSpecificRecord || typeof(GenericRecord).IsAssignableFrom(valueType))
            {
                if (string.IsNullOrWhiteSpace(specifiedAvroSchema))
                {
                    if (!isSpecificRecord)
                    {
                        throw new ArgumentNullException(nameof(specifiedAvroSchema), $@"parameter is required when creating an generic avro serializer");
                    }

                    specifiedAvroSchema = ((ISpecificRecord)Activator.CreateInstance(valueType)).Schema.ToString();
                }

                var schemaRegistry = new LocalSchemaRegistry(specifiedAvroSchema);
                return(Activator.CreateInstance(typeof(AvroSerializer <>).MakeGenericType(valueType), schemaRegistry, null /* config */));
            }

            return(null);
        }
Beispiel #2
0
        internal static object ResolveValueDeserializer(Type valueType, string specifiedAvroSchema)
        {
            if (typeof(Google.Protobuf.IMessage).IsAssignableFrom(valueType))
            {
                return(Activator.CreateInstance(typeof(ProtobufDeserializer <>).MakeGenericType(valueType)));
            }

            var isSpecificRecord = typeof(ISpecificRecord).IsAssignableFrom(valueType);

            if (isSpecificRecord || typeof(GenericRecord).IsAssignableFrom(valueType))
            {
                if (string.IsNullOrWhiteSpace(specifiedAvroSchema))
                {
                    if (!isSpecificRecord)
                    {
                        throw new ArgumentNullException(nameof(specifiedAvroSchema), $@"parameter is required when creating an generic avro deserializer");
                    }

                    specifiedAvroSchema = ((ISpecificRecord)Activator.CreateInstance(valueType)).Schema.ToString();
                }

                var methodInfo    = typeof(SerializationHelper).GetMethod(nameof(CreateAvroDeserializer), BindingFlags.Static | BindingFlags.NonPublic);
                var genericMethod = methodInfo.MakeGenericMethod(valueType);

                var schemaRegistry = new LocalSchemaRegistry(specifiedAvroSchema);
                return(genericMethod.Invoke(null, new object[] { schemaRegistry }));
            }

            return(null);
        }
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            var schemaRegistry = new LocalSchemaRegistry(avroSchema);
            AvroDeserializer <TValue> avroDeserializer = new AvroDeserializer <TValue>(schemaRegistry);

            SetConsumerAndExecutor(avroDeserializer, null, null);

            return(Task.CompletedTask);
        }
Beispiel #4
0
        public KafkaProducer(
            ProducerConfig config,
            string avroSchema,
            ILogger logger)
        {
            this.logger = logger;
            var builder = new ProducerBuilder <TKey, TValue>(config);

            IAsyncSerializer <TValue> asyncValueSerializer = null;
            ISerializer <TValue>      valueSerializer      = null;
            IAsyncSerializer <TKey>   keySerializer        = null;

            if (!string.IsNullOrEmpty(avroSchema))
            {
                var schemaRegistry = new LocalSchemaRegistry(avroSchema);
                asyncValueSerializer = new AvroSerializer <TValue>(schemaRegistry);
            }
            else
            {
                if (typeof(Google.Protobuf.IMessage).IsAssignableFrom(typeof(TValue)))
                {
                    // protobuf: need to create using reflection due to generic requirements in ProtobufSerializer
                    valueSerializer = (ISerializer <TValue>)Activator.CreateInstance(typeof(ProtobufSerializer <>).MakeGenericType(typeof(TValue)));
                }
            }

            if (asyncValueSerializer != null)
            {
                builder.SetValueSerializer(asyncValueSerializer);
            }
            else if (valueSerializer != null)
            {
                builder.SetValueSerializer(valueSerializer);
            }

            if (keySerializer != null)
            {
                builder.SetKeySerializer(keySerializer);
            }

            this.producer = builder.Build();
        }