Пример #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>("ServiceDiscoveryAddress"),
                ServiceAddress          = configuration.GetValue <Uri>("ServiceAddress"),
                ServiceName             = configuration.GetValue <string>("ServiceName"),
                ServiceId = configuration.GetValue <string>("ServiceId")
            };

            return(serviceConfig);
        }
Пример #2
0
        public static void RegisterServiceDiscovery(this IServiceCollection services, ServiceConfig serviceConfig)
        {
            if (serviceConfig == null)
            {
                throw new ArgumentNullException(nameof(serviceConfig));
            }

            var consulClient = CreateConsulClient(serviceConfig);

            services.AddSingleton(serviceConfig);
            services.AddSingleton <IHostedService, ServiceDiscoveryService>();
            services.AddSingleton <IConsulClient, ConsulClient>(p => consulClient);
        }