/// <summary>
        /// Registers the servicestack apphost with the local consul agent
        /// </summary>
        /// <exception cref="GatewayServiceDiscoveryException">throws exception if registration was not successful</exception>
        public static void RegisterService(ServiceRegistration registration)
        {
            var consulServiceRegistration = new ConsulServiceRegistration(registration.Id, registration.Name)
            {
                Address = registration.Address,
                Tags    = registration.Tags,
                Port    = registration.Port
            };

            ServiceValidator.ValidateAndThrow(consulServiceRegistration);

            var registrationUrl = ConsulUris.LocalAgent.CombineWith(consulServiceRegistration.ToPutUrl());

            registrationUrl.PutJsonToUrl(consulServiceRegistration, null,
                                         response =>
            {
                var logger = LogManager.GetLogger(typeof(ConsulClient));

                if (response.StatusCode.IsErrorResponse())
                {
                    logger.Fatal(
                        $"Could not register appHost with Consul. It will not be discoverable: {consulServiceRegistration}");
                    throw new GatewayServiceDiscoveryException("Failed to register service with consul");
                }
                else
                {
                    logger.Info($"Registered service with Consul {consulServiceRegistration}");
                    AppDomain.CurrentDomain.ProcessExit +=
                        (sender, args) => UnregisterService(consulServiceRegistration.ID);
                    AppDomain.CurrentDomain.UnhandledException +=
                        (sender, args) => UnregisterService(consulServiceRegistration.ID);
                }
            });
        }
        /// <summary>
        /// Registers the servicestack apphost with the local consul agent
        /// </summary>
        /// <exception cref="GatewayServiceDiscoveryException">throws exception if registration was not successful</exception>
        public static void RegisterService(ServiceRegistration registration)
        {
            var consulServiceRegistration = new ConsulServiceRegistration(registration.Id, registration.Name)
                                   {
                                        Address = registration.Address,
                                        Tags = registration.Tags,
                                        Port = registration.Port
                                   };

            ServiceValidator.ValidateAndThrow(consulServiceRegistration);

            var registrationUrl = ConsulUris.LocalAgent.CombineWith(consulServiceRegistration.ToPutUrl());
            registrationUrl.PostJsonToUrl(consulServiceRegistration, null,
                response =>
                {
                    var logger = LogManager.GetLogger(typeof(ConsulClient));
                    if (response.StatusCode.IsErrorResponse())
                    {
                        logger.Fatal(
                            $"Could not register appHost with Consul. It will not be discoverable: {consulServiceRegistration}");
                        throw new GatewayServiceDiscoveryException("Failed to register service with consul");
                    }
                    else
                    {
                        logger.Info($"Registered service with Consul {consulServiceRegistration}");
                        AppDomain.CurrentDomain.ProcessExit +=
                            (sender, args) => UnregisterService(consulServiceRegistration.ID);
                        AppDomain.CurrentDomain.UnhandledException +=
                            (sender, args) => UnregisterService(consulServiceRegistration.ID);
                    }
                });
        }