Exemple #1
0
        /// <summary>
        /// Maps the controller and health check end points.
        /// </summary>
        /// <param name="app">The application builder. </param>
        /// <param name="serviceName">The service name. </param>
        /// <returns>Returns <see cref="IApplicationBuilder"/> to chain further upon.</returns>
        public static IApplicationBuilder MapControllerAndHealthCheckEndPoints(this IApplicationBuilder app, string serviceName)
        {
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks($"{serviceName}/health", new HealthCheckOptions()
                {
                    ResponseWriter = async(context, result) =>
                    {
                        IMessageSerializer messageSerializer = app.ApplicationServices.GetService <IMessageSerializer>();
                        IMapper mapper = app.ApplicationServices.GetService <IMapper>();
                        Guid requestId = Guid.Empty;
                        ServiceHealthCheckResponse response;
                        if (context.Request.Headers.ContainsKey("Request-Id") && Guid.TryParse(context.Request.Headers["Request-Id"].FirstOrDefault(), out requestId))
                        {
                            ServiceHealthReport report = result.ToServiceHealthReport();
                            response = new ServiceHealthCheckResponse(requestId, mapper.Map <ServiceHealthReportDto>(report), "", true);
                        }
                        else
                        {
                            response = new ServiceHealthCheckResponse(requestId, null, "Please provide a 'Request-Id' header with request id as valid guid.", false);
                        }

                        string json = messageSerializer.SerializeToString(response);
                        context.Response.ContentType = $"application/{messageSerializer.SerializerType}";
                        await context.Response.WriteAsync(json);
                    }
                });
            });
            return(app);
        }
        /// <inheritdoc cref="IRegistryService.AddServiceAsync(ServiceDto)"/>
        public async Task <bool> AddServiceAsync(ServiceDto service)
        {
            _logger.LogInformation("[STARTED] [AddServiceAsync]");
            try
            {
                string        json          = _messageSerializer.SerializeToString(service);
                StringContent stringContent = new StringContent(json, Encoding.UTF8, "application/json");
                _logger.LogInformation($"[CONTENT] [AddServiceAsync] StringContent is:  {json}");
                HttpResponseMessage responseMessage = await _httpClient.PostAsync("registry", stringContent);

                _logger.LogInformation($"[Response] [AddServiceAsync] Response status code: {responseMessage.StatusCode.ToString()}");
                return(responseMessage.IsSuccessStatusCode);
            }
            catch (Exception e)
            {
                _logger.LogError($"[ERROR] [AddServiceAsync] {e.Message}");
                return(false);
            }
            finally
            {
                _logger.LogInformation("[FINISHED] [AddServiceAsync]");
            }
        }
Exemple #3
0
        public async Task SendMessageToTopicAsync <TMessage>(string topicName, TMessage messageContent, string correlationId = null)
        {
            var config = new ProducerConfig {
                BootstrapServers = configBasedConnectionDetails.Server, ClientId = uniqueClientName
            };

            // A Producer for sending messages with null keys and UTF-8 encoded values.
            using (var p = new Producer <Null, string>(config))
            {
                try
                {
                    var dr = await p.ProduceAsync(topicName, new Message <Null, string> {
                        Value = serializer.SerializeToString(messageContent)
                    });

                    log.Debug($"Sent '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                }
                catch (KafkaException e)
                {
                    log.Error(e, $"Delivery failed: {e.Error.Reason}");
                    await Task.FromException(e);
                }
            }
        }