public SpringBootAdminClientHostedService(SpringBootAdminClientOptions options, ManagementEndpointOptions mgmtOptions, HealthEndpointOptions healthOptions, HttpClient httpClient = null, ILogger logger = null)
 {
     _options       = options;
     _mgmtOptions   = mgmtOptions;
     _healthOptions = healthOptions;
     _httpClient    = httpClient ?? HttpClientHelper.GetHttpClient(_options.ValidateCertificates, _options.ConnectionTimeoutMS);
     _logger        = logger ?? NullLogger.Instance;
 }
        /// <summary>
        /// Register the application with a Spring-Boot-Admin server
        /// </summary>
        /// <param name="builder"><see cref="IApplicationBuilder"/></param>
        /// <param name="configuration">App configuration. Will be retrieved from builder.ApplicationServices if not provided</param>
        /// <param name="httpClient">A customized HttpClient. [Bring your own auth]</param>
        public static void RegisterWithSpringBootAdmin(this IApplicationBuilder builder, IConfiguration configuration = null, HttpClient httpClient = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (configuration is null)
            {
                configuration = builder.ApplicationServices.GetRequiredService <IConfiguration>();
            }

            var logger        = builder.ApplicationServices.GetService <ILogger <SpringBootAdminClientOptions> >();
            var appInfo       = builder.ApplicationServices.GetApplicationInstanceInfo();
            var options       = new SpringBootAdminClientOptions(configuration, appInfo);
            var mgmtOptions   = new ManagementEndpointOptions(configuration);
            var healthOptions = new HealthEndpointOptions(configuration);
            var basePath      = options.BasePath.TrimEnd('/');

            httpClient ??= HttpClientHelper.GetHttpClient(options.ValidateCertificates, ConnectionTimeoutMs);

            var app = new Application()
            {
                Name          = options.ApplicationName ?? "Steeltoe",
                HealthUrl     = new Uri($"{basePath}{mgmtOptions.Path}/{healthOptions.Path}"),
                ManagementUrl = new Uri($"{basePath}{mgmtOptions.Path}"),
                ServiceUrl    = new Uri($"{basePath}/"),
                Metadata      = new Dictionary <string, object> {
                    { "startup", DateTime.Now }
                },
            };

            app.Metadata.Merge(options.Metadata);

            var lifetime = builder.ApplicationServices.GetService <IHostApplicationLifetime>();

            lifetime.ApplicationStarted.Register(() =>
            {
                logger?.LogInformation("Registering with Spring Boot Admin Server at {0}", options.Url);

                var result = httpClient.PostAsJsonAsync($"{options.Url}/instances", app).GetAwaiter().GetResult();
                if (result.IsSuccessStatusCode)
                {
                    RegistrationResult = result.Content.ReadFromJsonAsync <RegistrationResult>().GetAwaiter().GetResult();
                }
                else
                {
                    logger.LogError($"Error registering with SpringBootAdmin {result}");
                }
            });

            lifetime.ApplicationStopped.Register(() =>
            {
                if (RegistrationResult == null || string.IsNullOrEmpty(RegistrationResult.Id))
                {
                    return;
                }

                _ = httpClient.DeleteAsync($"{options.Url}/instances/{RegistrationResult.Id}").GetAwaiter().GetResult();
            });
        }