forked from G-Research/consuldotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
63 lines (56 loc) · 2.56 KB
/
Copy pathServiceCollectionExtensions.cs
File metadata and controls
63 lines (56 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Consul.AspNetCore
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// Add default <see cref="IConsulClient"/>.
/// First client can be accessed from DI, to create multiple named clients use <see cref="IConsulClientFactory"/>
/// </summary>
public static IServiceCollection AddConsul(this IServiceCollection services)
{
return services.AddConsul(options => { });
}
/// <summary>
/// Add default <see cref="IConsulClient"/> with configured <see cref="IOptions{ConsulClientConfiguration}"/>.
/// First client can be accessed from DI, to create multiple named clients use <see cref="IConsulClientFactory"/>
/// </summary>
public static IServiceCollection AddConsul(
this IServiceCollection services,
Action<ConsulClientConfiguration> configure)
{
return services.AddConsul(Options.DefaultName, configure);
}
/// <summary>
/// Add named <see cref="IConsulClient"/> with configured <see cref="IOptions{ConsulClientConfiguration}"/>.
/// First client can be accessed from DI, to create multiple named clients use <see cref="IConsulClientFactory"/>
/// </summary>
public static IServiceCollection AddConsul(
this IServiceCollection services,
string name,
Action<ConsulClientConfiguration> configure)
{
services.Configure(name, configure);
services.TryAddSingleton<IConsulClientFactory, ConsulClientFactory>();
services.TryAddSingleton(sp => sp.GetRequiredService<IConsulClientFactory>().CreateClient(name));
return services;
}
/// <summary>
/// Register consul service with default <see cref="IConsulClient"/>.
/// First client can be accessed from DI, to create multiple named clients use <see cref="IConsulClientFactory"/>
/// </summary>
public static IServiceCollection AddConsulServiceRegistration(
this IServiceCollection services,
Action<AgentServiceRegistration> configure)
{
var registration = new AgentServiceRegistration();
configure.Invoke(registration);
return services
.AddSingleton(registration)
.AddHostedService<AgentServiceRegistrationHostedService>();
}
}
}