public static IConveyBuilder AddConsul(this IConveyBuilder builder, ConsulOptions options, HttpClientOptions httpClientOptions) { builder.Services.AddSingleton(options); if (!options.Enabled || !builder.TryRegister(RegistryName)) { return(builder); } if (httpClientOptions.Type?.ToLowerInvariant() == "consul") { builder.Services.AddTransient <ConsulServiceDiscoveryMessageHandler>(); builder.Services.AddHttpClient <IConsulHttpClient, ConsulHttpClient>("consul-http") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); builder.RemoveHttpClient(); builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>("consul") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); } builder.Services.AddTransient <IConsulServicesRegistry, ConsulServicesRegistry>(); var registration = builder.CreateConsulAgentRegistration(options); if (registration is null) { return(builder); } builder.Services.AddSingleton(registration); return(builder); }
private static IConveyBuilder AddFabio(this IConveyBuilder builder, FabioOptions fabioOptions, HttpClientOptions httpClientOptions, Action <IConveyBuilder> registerConsul) { registerConsul(builder); builder.Services.AddSingleton(fabioOptions); if (!fabioOptions.Enabled || !builder.TryRegister(RegistryName)) { return(builder); } if (httpClientOptions.Type?.ToLowerInvariant() == "fabio") { builder.Services.AddTransient <FabioMessageHandler>(); builder.Services.AddHttpClient <IFabioHttpClient, FabioHttpClient>("fabio-http") .AddHttpMessageHandler <FabioMessageHandler>(); builder.RemoveHttpClient(); builder.Services.AddHttpClient <IHttpClient, FabioHttpClient>("fabio") .AddHttpMessageHandler <FabioMessageHandler>(); } using (var serviceProvider = builder.Services.BuildServiceProvider()) { var registration = serviceProvider.GetService <AgentServiceRegistration>(); registration.Tags = GetFabioTags(registration.Name, fabioOptions.Service); builder.Services.UpdateConsulRegistration(registration); } return(builder); }
public static IConveyBuilder AddConsul(this IConveyBuilder builder, ConsulOptions options, HttpClientOptions httpClientOptions) { builder.Services.AddSingleton(options); if (!options.Enabled || !builder.TryRegister(RegistryName)) { return(builder); } if (httpClientOptions.Type?.ToLowerInvariant() == "consul") { builder.Services.AddTransient <ConsulServiceDiscoveryMessageHandler>(); builder.Services.AddHttpClient <IConsulHttpClient, ConsulHttpClient>("consul-http") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); builder.RemoveHttpClient(); builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>("consul") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); } builder.Services.AddTransient <IConsulServicesRegistry, ConsulServicesRegistry>(); builder.Services.AddSingleton <IConsulClient>(c => new ConsulClient(cfg => { if (!string.IsNullOrEmpty(options.Url)) { cfg.Address = new Uri(options.Url); } })); var registration = builder.CreateConsulAgentRegistration(options); if (registration is null) { return(builder); } builder.Services.AddSingleton(registration); builder.AddBuildAction(sp => { var consulRegistration = sp.GetService <AgentServiceRegistration>(); var client = sp.GetService <IConsulClient>(); client.Agent.ServiceRegister(consulRegistration); }); return(builder); }