Exemplo n.º 1
0
        public static ServiceConfig GetServiceConfig(this IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var serviceConfig = new ServiceConfig
            {
                ServiceDiscoveryAddress = configuration.GetValue <Uri>("ServiceConfig:serviceDiscoveryAddress"),
                ServiceAddress          = configuration.GetValue <Uri>("ServiceConfig:serviceAddress"),
                ServiceName             = configuration.GetValue <string>("ServiceConfig:serviceName"),
                ServiceId = configuration.GetValue <string>("ServiceConfig:serviceId")
            };

            return(serviceConfig);
        }
        public static IServiceCollection AddServiceDiscovery(this IServiceCollection services, ServiceConfig serviceConfig)
        {
            if (serviceConfig == null)
            {
                throw new ArgumentNullException(nameof(serviceConfig));
            }

            var consulClient = new ConsulClient(config =>
            {
                config.Address = serviceConfig.ServiceDiscoveryAddress;
            });

            services.AddSingleton(serviceConfig);
            services.AddHostedService <ServiceDiscoveryHostedService>();
            services.AddSingleton <IConsulClient, ConsulClient>(p => consulClient);

            return(services);
        }
Exemplo n.º 3
0
 public ServiceDiscoveryHostedService(IConsulClient client, ServiceConfig config)
 {
     _client = client;
     _config = config;
 }