Provides extensions to HttpClient to resolve well-known hostnames to concrete endpoints based on DNS records. Useful for service resolution in orchestrators such as Kubernetes.
$ dotnet add package Microsoft.Extensions.ServiceDiscovery.DnsThis library provides support for resolving service endpoints using DNS (Domain Name System). It provides two service endpoint providers:
DNS, which resolves endpoints using DNS A/AAAA record queries. This means that it can resolve names to IP addresses, but cannot resolve port numbers endpoints. As such, port numbers are assumed to be the default for the protocol (for example, 80 for HTTP and 433 for HTTPS). The benefit of using the DNS provider is that for cases where these default ports are appropriate, clients can spread their requests across hosts. For more information, see Load-balancing with endpoint selectors.
DNS SRV, which resolves service names using DNS SRV record queries. This allows it to resolve both IP addresses and port numbers. This is useful for environments which support DNS SRV queries, such as Kubernetes (when configured accordingly).
The DNS service endpoint provider resolves endpoints using DNS A/AAAA record queries. This means that it can resolve names to IP addresses, but cannot resolve port numbers endpoints. As such, port numbers are assumed to be the default for the protocol (for example, 80 for HTTP and 433 for HTTPS). The benefit of using the DNS service endpoint provider is that for cases where these default ports are appropriate, clients can spread their requests across hosts. For more information, see Load-balancing with endpoint selectors.
To configure the DNS service endpoint provider in your application, add the DNS service endpoint provider to your host builder's service collection using the AddDnsServiceEndpointProvider method. service discovery as follows:
builder.Services.AddServiceDiscoveryCore();
builder.Services.AddDnsServiceEndpointProvider();
When deploying to Kubernetes, the DNS SRV service endpoint provider can be used to resolve endpoints. For example, the following resource definition will result in a DNS SRV record being created for an endpoint named "default" and an endpoint named "dashboard", both on the service named "basket".
apiVersion: v1
kind: Service
metadata:
name: basket
spec:
selector:
name: basket-service
clusterIP: None
ports:
- name: default
port: 8080
- name: dashboard
port: 8888
To configure a service to resolve the "dashboard" endpoint on the "basket" service, add the DNS SRV service endpoint provider to the host builder as follows:
builder.Services.AddServiceDiscoveryCore();
builder.Services.AddDnsSrvServiceEndpointProvider();
The special port name "default" is used to specify the default endpoint, resolved using the URI http://basket.
As in the previous example, add service discovery to an HttpClient for the basket service:
builder.Services.AddHttpClient<BasketServiceClient>(
static client => client.BaseAddress = new("http://basket"));
Similarly, the "dashboard" endpoint can be targeted as follows:
builder.Services.AddHttpClient<BasketServiceDashboardClient>(
static client => client.BaseAddress = new("http://_dashboard.basket"));