Skip to content

Commit 2c1b7aa

Browse files
authored
Fix up xref issues, and remove section (#40434)
* Fix up xref issues, and remove section * More fixes and corrections * Fix last UseServiceDiscovery
1 parent 09afefc commit 2c1b7aa

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

docs/core/extensions/service-discovery.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Service discovery in .NET
33
description: Learn how to use the Microsoft.Extensions.ServiceDiscovery library to simplify the integration of service discovery patterns in .NET applications.
44
author: IEvangelist
55
ms.author: dapine
6-
ms.date: 12/11/2023
6+
ms.date: 04/10/2024
77
ms.topic: overview
88
---
99

@@ -34,20 +34,20 @@ For more information, see [dotnet add package](../tools/dotnet-add-package.md) o
3434

3535
## Example usage
3636

37-
In the _Program.cs_ file of your project, call the <xref:Microsoft.Extensions.Hosting.HostingExtensions.AddServiceDiscovery%2A> extension method to add service discovery to the host, configuring default service endpoint resolvers:
37+
In the _Program.cs_ file of your project, call the <xref:Microsoft.Extensions.DependencyInjection.ServiceDiscoveryHttpClientBuilderExtensions.AddServiceDiscovery%2A> extension method to add service discovery to the host, configuring default service endpoint resolvers:
3838

3939
```csharp
4040
builder.Services.AddServiceDiscovery();
4141
```
4242

43-
Add service discovery to an individual <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> by calling the `UseServiceDiscovery` extension method:
43+
Add service discovery to an individual <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> by calling the `AddServiceDiscovery` extension method:
4444

4545
```csharp
4646
builder.Services.AddHttpClient<CatalogServiceClient>(static client =>
4747
{
48-
client.BaseAddress = new("http://catalog");
48+
client.BaseAddress = new("https://catalog");
4949
})
50-
.UseServiceDiscovery();
50+
.AddServiceDiscovery();
5151
```
5252

5353
Alternatively, you can add service discovery to all <xref:System.Net.Http.HttpClient> instances by default:
@@ -56,10 +56,31 @@ Alternatively, you can add service discovery to all <xref:System.Net.Http.HttpCl
5656
builder.Services.ConfigureHttpClientDefaults(static http =>
5757
{
5858
// Turn on service discovery by default
59-
http.UseServiceDiscovery();
59+
http.AddServiceDiscovery();
6060
});
6161
```
6262

63+
## Scheme selection when resolving HTTP(S) endpoints
64+
65+
It is common to use HTTP while developing and testing a service locally and HTTPS when the service is deployed. Service Discovery supports this by allowing for a priority list of URI schemes to be specified in the input string given to Service Discovery. Service Discovery will attempt to resolve the services for the schemes in order and will stop after an endpoint is found. URI schemes are separated by a `+` character, for example: `"https+http://basket"`. Service Discovery will first try to find HTTPS endpoints for the `"basket"` service and will then fall back to HTTP endpoints. If any HTTPS endpoint is found, Service Discovery will not include HTTP endpoints.
66+
67+
Schemes can be filtered by configuring the `AllowedSchemes` and `AllowAllSchemes` properties on `ServiceDiscoveryOptions`. The `AllowAllSchemes` property is used to indicate that all schemes are allowed. By default, `AllowAllSchemes` is `true` and all schemes are allowed. Schemes can be restricted by setting `AllowAllSchemes` to `false` and adding allowed schemes to the `AllowedSchemes` property. For example, to allow only HTTPS:
68+
69+
```csharp
70+
services.Configure<ServiceDiscoveryOptions>(options =>
71+
{
72+
options.AllowAllSchemes = false;
73+
options.AllowedSchemes = ["https"];
74+
});
75+
```
76+
77+
To explicitly allow all schemes, set the `ServiceDiscoveryOptions.AllowAllSchemes` property to `true`:
78+
79+
```csharp
80+
services.Configure<ServiceDiscoveryOptions>(
81+
options => options.AllowAllSchemes = true);
82+
```
83+
6384
## Resolve service endpoints from configuration
6485

6586
The `AddServiceDiscovery` extension method adds a configuration-based endpoint resolver by default.
@@ -70,21 +91,23 @@ Here's an example demonstrating how to configure endpoints for the service named
7091
```json
7192
{
7293
"Services": {
73-
"catalog": [
94+
"catalog": {
95+
"https": [
7496
"localhost:8080",
75-
"10.46.24.90:80",
97+
"10.46.24.90:80"
7698
]
7799
}
100+
}
78101
}
79102
```
80103

81-
The preceding example adds two endpoints for the service named _catalog_: `localhost:8080`, and `"10.46.24.90:80"`. Each time the _catalog_ is resolved, one of these endpoints is selected.
104+
The preceding example adds two endpoints for the service named _catalog_: `https://localhost:8080`, and `"https://10.46.24.90:80"`. Each time the _catalog_ is resolved, one of these endpoints is selected.
82105

83-
If service discovery was added to the host using the <xref:Microsoft.Extensions.Hosting.HostingExtensions.AddServiceDiscoveryCore%2A> extension method on <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection>, the configuration-based endpoint resolver can be added by calling the <xref:Microsoft.Extensions.Hosting.HostingExtensions.AddConfigurationServiceEndPointResolver%2A> extension method on `IServiceCollection`.
106+
If service discovery was added to the host using the <xref:Microsoft.Extensions.DependencyInjection.ServiceDiscoveryServiceCollectionExtensions.AddServiceDiscoveryCore%2A> extension method on <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection>, the configuration-based endpoint resolver can be added by calling the <xref:Microsoft.Extensions.DependencyInjection.ServiceDiscoveryServiceCollectionExtensions.AddConfigurationServiceEndPointResolver%2A> extension method on `IServiceCollection`.
84107

85108
### Configuration
86109

87-
The configuration resolver is configured using the <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.ConfigurationServiceEndPointResolverOptions> class, which offers these configuration options:
110+
The configuration resolver is configured using the <xref:Microsoft.Extensions.ServiceDiscovery.ConfigurationServiceEndPointResolverOptions> class, which offers these configuration options:
88111

89112
- **SectionName**: The name of the configuration section that contains service endpoints. It defaults to `"Services"`.
90113

@@ -120,31 +143,7 @@ The pass-through resolver performs no external resolution and instead resolves e
120143

121144
The pass-through provider is configured by-default when adding service discovery via the `AddServiceDiscovery` extension method.
122145

123-
If service discovery was added to the host using the `AddServiceDiscoveryCore` extension method on `IServiceCollection`, the pass-through provider can be added by calling the <xref:Microsoft.Extensions.Hosting.HostingExtensions.AddPassThroughServiceEndPointResolver%2A> extension method on `IServiceCollection`.
124-
125-
## Load-balancing with endpoint selectors
126-
127-
Each time an endpoint is resolved via the `HttpClient` pipeline, a single endpoint is selected from the set of all known endpoints for the requested service. If multiple endpoints are available, it may be desirable to balance traffic across all such endpoints. To accomplish this, a customizable _endpoint selector_ can be used. By default, endpoints are selected in round-robin order. To use a different endpoint selector, provide an <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelector> instance to the <xref:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.UseServiceDiscovery%2A> method call. For example, to select a random endpoint from the set of resolved endpoints, specify <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.RandomServiceEndPointSelectorProvider.Instance?displayProperty=nameWithType> as the endpoint selector:
128-
129-
```csharp
130-
builder.Services.AddHttpClient<CatalogServiceClient>(
131-
static client => client.BaseAddress = new("http://catalog")
132-
)
133-
.UseServiceDiscovery(RandomServiceEndPointSelectorProvider.Instance);
134-
```
135-
136-
The `Microsoft.Extensions.ServiceDiscovery` package includes the following endpoint selector providers:
137-
138-
- <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.PickFirstServiceEndPointSelectorProvider.Instance?displayProperty=nameWithType>: Pick-first, which always selects the first endpoint.
139-
- <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.RoundRobinServiceEndPointSelectorProvider.Instance?displayProperty=nameWithType>: Round-robin, which cycles through endpoints.
140-
- <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.RandomServiceEndPointSelectorProvider.Instance?displayProperty=nameWithType>: Random, which selects endpoints randomly.
141-
- <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.PowerOfTwoChoicesServiceEndPointSelectorProvider.Instance?displayProperty=nameWithType>: Power-of-two-choices, which attempt to pick the least used endpoint based on the _Power of Two Choices_ algorithm for distributed load balancing, degrading to randomly selecting an endpoint when either of the provided endpoints don't have the <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IEndPointLoadFeature> feature.
142-
143-
Endpoint selectors are created via an <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelectorProvider> instance, such as the providers previously listed. The provider's <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelectorProvider.CreateSelector> method is called to create a selector, which is an instance of <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelector>. The `IServiceEndPointSelector` instance is given the set of known endpoints when they're resolved, using the <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelector.SetEndPoints%2A?displayProperty=nameWithType> method. To choose an endpoint from the collection, the <xref:Microsoft.Extensions.ServiceDiscovery.Abstractions.IServiceEndPointSelector.GetEndPoint%2A?displayProperty=nameWithType> method is called, returning a single `ServiceEndPoint`. The `context` value passed to `GetEndPoint` is used to provide extra context that may be useful to the selector. For example, in the `HttpClient` case, the <xref:System.Net.Http.HttpRequestMessage> is passed. None of the provided implementations of `IServiceEndPointSelector` inspect the context, and it can be ignored unless you're using a selector, which does make use of it.
144-
145-
### Resolution order
146-
147-
When service endpoints are being resolved, each registered resolver is called in the order of registration and given the opportunity to modify the collection of `ServiceEndPoint`s which are returned back to the caller. The providers included in the `Microsoft.Extensions.ServiceDiscovery` series of packages skip resolution if there are existing endpoints in the collection when they're called. For example, consider a case where the following providers are registered: _Configuration_, _DNS SRV_, _Pass-through_. When resolution occurs, the providers are called in-order. If the _Configuration_ providers discover no endpoints, the _DNS SRV_ provider performs resolution and may add one or more endpoints. If the _DNS SRV_ provider adds an endpoint to the collection, the _Pass-through_ provider skips its resolution and returns immediately instead.
146+
If service discovery was added to the host using the `AddServiceDiscoveryCore` extension method on `IServiceCollection`, the pass-through provider can be added by calling the <xref:Microsoft.Extensions.DependencyInjection.ServiceDiscoveryServiceCollectionExtensions.AddPassThroughServiceEndPointResolver%2A> extension method on `IServiceCollection`.
148147

149148
## See also
150149

0 commit comments

Comments
 (0)