public override Task <OperateResult> PublishAsync(string keyName, string content) { try { var config = _kafkaOptions.AskafkaConfig(); var contentBytes = Encoding.UTF8.GetBytes(content); using (var producer = new Producer(config)) { var message = producer.ProduceAsync(keyName, null, contentBytes).Result; if (!message.Error.HasError) { _logger.LogDebug($"kafka topic message [{keyName}] has been published."); return(Task.FromResult(OperateResult.Success)); } else { return(Task.FromResult(OperateResult.Failed(new OperateError { Code = message.Error.Code.ToString(), Description = message.Error.Reason }))); } } } catch (Exception ex) { _logger.LogError($"kafka topic message [{keyName}] has benn raised an exception of sending. the exception is: {ex.Message}"); return(Task.FromResult(OperateResult.Failed(ex))); } }
private void InitKafkaClient() { _kafkaOptions.MainConfig.Add("group.id", _groupId); var config = _kafkaOptions.AskafkaConfig(); _consumerClient = new Consumer <Null, string>(config, null, StringDeserializer); _consumerClient.OnMessage += ConsumerClient_OnMessage; _consumerClient.OnError += ConsumerClient_OnError; }