-
Notifications
You must be signed in to change notification settings - Fork 394
/
ServiceDiscoveryDnsServiceCollectionExtensions.cs
75 lines (69 loc) · 4.02 KB
/
ServiceDiscoveryDnsServiceCollectionExtensions.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using DnsClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.ServiceDiscovery;
using Microsoft.Extensions.ServiceDiscovery.Dns;
namespace Microsoft.Extensions.Hosting;
/// <summary>
/// Extensions for <see cref="IServiceCollection"/> to add service discovery.
/// </summary>
public static class ServiceDiscoveryDnsServiceCollectionExtensions
{
/// <summary>
/// Adds DNS SRV service discovery to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <returns>The provided <see cref="IServiceCollection"/>.</returns>
/// <remarks>
/// DNS SRV queries are able to provide port numbers for endpoints and can support multiple named endpoints per service.
/// However, not all environment support DNS SRV queries, and in some environments, additional configuration may be required.
/// </remarks>
public static IServiceCollection AddDnsSrvServiceEndpointProvider(this IServiceCollection services) => services.AddDnsSrvServiceEndpointProvider(_ => { });
/// <summary>
/// Adds DNS SRV service discovery to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configureOptions">The DNS SRV service discovery configuration options.</param>
/// <returns>The provided <see cref="IServiceCollection"/>.</returns>
/// <remarks>
/// DNS SRV queries are able to provide port numbers for endpoints and can support multiple named endpoints per service.
/// However, not all environment support DNS SRV queries, and in some environments, additional configuration may be required.
/// </remarks>
public static IServiceCollection AddDnsSrvServiceEndpointProvider(this IServiceCollection services, Action<DnsSrvServiceEndpointProviderOptions> configureOptions)
{
services.AddServiceDiscoveryCore();
services.TryAddSingleton<IDnsQuery, LookupClient>();
services.AddSingleton<IServiceEndpointProviderFactory, DnsSrvServiceEndpointProviderFactory>();
var options = services.AddOptions<DnsSrvServiceEndpointProviderOptions>();
options.Configure(o => configureOptions?.Invoke(o));
return services;
}
/// <summary>
/// Adds DNS service discovery to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <returns>The provided <see cref="IServiceCollection"/>.</returns>
/// <remarks>
/// DNS A/AAAA queries are widely available but are not able to provide port numbers for endpoints and cannot support multiple named endpoints per service.
/// </remarks>
public static IServiceCollection AddDnsServiceEndpointProvider(this IServiceCollection services) => services.AddDnsServiceEndpointProvider(_ => { });
/// <summary>
/// Adds DNS service discovery to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configureOptions">The DNS SRV service discovery configuration options.</param>
/// <returns>The provided <see cref="IServiceCollection"/>.</returns>
/// <remarks>
/// DNS A/AAAA queries are widely available but are not able to provide port numbers for endpoints and cannot support multiple named endpoints per service.
/// </remarks>
public static IServiceCollection AddDnsServiceEndpointProvider(this IServiceCollection services, Action<DnsServiceEndpointProviderOptions> configureOptions)
{
services.AddServiceDiscoveryCore();
services.AddSingleton<IServiceEndpointProviderFactory, DnsServiceEndpointProviderFactory>();
var options = services.AddOptions<DnsServiceEndpointProviderOptions>();
options.Configure(o => configureOptions?.Invoke(o));
return services;
}
}