Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ In a Minimal API app, there are two different built-in centralized mechanisms to

This section refers to the following Minimal API app to demonstrate ways to handle exceptions. It throws an exception when the endpoint `/exception` is requested:

``` csharp
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

Expand Down Expand Up @@ -65,7 +65,7 @@ In non-development environments, use the [Exception Handler Middleware](xref:fun
For example, the following code changes the app to respond with an [RFC 7807](https://tools.ietf.org/html/rfc7807)-compliant payload to the client. For more information, see [Problem Details](#problem-details) section.


``` csharp
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

Expand All @@ -84,7 +84,7 @@ app.Run();

Consider the following Minimal API app.

``` csharp
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

Expand All @@ -103,7 +103,7 @@ The [`Status Code Pages middleware`](xref:fundamentals/error-handling#sestatusco

For example, the following example changes the app to respond with an [RFC 7807](https://tools.ietf.org/html/rfc7807)-compliant payload to the client for all client and server responses, including routing errors (for example, `404 NOT FOUND`). For more information, see the [Problem Details](#problem-details) section.

``` csharp
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

Expand All @@ -127,7 +127,7 @@ Minimal API apps can be configured to generate problem details response for all

The following code configures the app to generate problem details:

``` csharp
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddProblemDetails();

Expand Down
2 changes: 1 addition & 1 deletion aspnetcore/fundamentals/servers/httpsys.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The preceding Windows 11 Build versions may require the use of a [Windows Inside

HTTP/3 is discovered as an upgrade from HTTP/1.1 or HTTP/2 via the `alt-svc` header. That means the first request will normally use HTTP/1.1 or HTTP/2 before switching to HTTP/3. Http.Sys doesn't automatically add the `alt-svc` header, it must be added by the application. The following code is a middleware example that adds the `alt-svc` response header.

```C#
```csharp
app.Use((context, next) =>
{
context.Response.Headers.AltSvc = "h3=\":443\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ The preceding Windows 11 Build versions may require the use of a [Windows Inside

HTTP/3 is discovered as an upgrade from HTTP/1.1 or HTTP/2 via the `alt-svc` header. That means the first request will normally use HTTP/1.1 or HTTP/2 before switching to HTTP/3. Http.Sys doesn't automatically add the `alt-svc` header, it must be added by the application. The following code is a middleware example that adds the `alt-svc` response header.

```C#
```csharp
app.Use((context, next) =>
{
context.Response.Headers.AltSvc = "h3=\":443\"";
Expand Down
2 changes: 1 addition & 1 deletion aspnetcore/fundamentals/servers/yarp/authn-authz.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ No authentication or authorization is performed on requests unless enabled in th
Authorization policies can be specified per route via [RouteConfig.AuthorizationPolicy](xref:Yarp.ReverseProxy.Configuration.RouteConfig) and can be bound from the `Routes` sections of the config file. As with other route properties, this can be modified and reloaded without restarting the proxy. Policy names are case insensitive.

Example:
```JSON
```json
{
"ReverseProxy": {
"Routes": {
Expand Down
10 changes: 5 additions & 5 deletions aspnetcore/fundamentals/servers/yarp/config-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The reverse proxy can load configuration for routes and clusters from files usin

## Loading Configuration
To load the proxy configuration from IConfiguration add the following code in Program.cs:
```c#
```csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -42,13 +42,13 @@ Configuration can be modified during the load sequence using [Configuration Filt
## Multiple Configuration Sources
As of 1.1, YARP supports loading the proxy configuration from multiple sources. LoadFromConfig may be called multiple times referencing different IConfiguration sections or may be combine with a different config source like InMemory. Routes can reference clusters from other sources. Note merging partial config from different sources for a given route or cluster is not supported.

```c#
```csharp
services.AddReverseProxy()
.LoadFromConfig(Configuration.GetSection("ReverseProxy1"))
.LoadFromConfig(Configuration.GetSection("ReverseProxy2"));
```
or
```c#
```csharp

services.AddReverseProxy()
.LoadFromMemory(routes, clusters)
Expand All @@ -62,7 +62,7 @@ File-based configuration is dynamically mapped to the types in [Yarp.ReverseProx
The configuration consists of a named section that you specified above via `Configuration.GetSection("ReverseProxy")`, and contains subsections for routes and clusters.

Example:
```JSON
```json
{
"ReverseProxy": {
"Routes": {
Expand Down Expand Up @@ -105,7 +105,7 @@ The clusters section is an unordered collection of named clusters. A cluster pri
For additional fields see [ClusterConfig](xref:Yarp.ReverseProxy.Configuration.ClusterConfig).

## All config properties
```JSON
```json
{
// Base URLs the server listens on, must be configured independently of the routes below
"Urls": "http://localhost:5000;https://localhost:5001",
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/fundamentals/servers/yarp/config-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Filters can be used for a variety of purposes such as:
## AddConfigFilter
Configuration filters are registered in the Dependency Injection system using the [AddConfigFilter](xref:Microsoft.Extensions.DependencyInjection.ReverseProxyServiceCollectionExtensions) API. Any number of unique filters can be added and will be applied in the order added.

```C#
```csharp
// Load the configuration and register a config filter
services.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"))
Expand All @@ -39,7 +39,7 @@ Filters are called for each route and cluster each time configuration is loaded

This example fills in destination addresses from environment variables and sets the route's Order field to 1.

```C#
```csharp
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
Expand Down
2 changes: 1 addition & 1 deletion aspnetcore/fundamentals/servers/yarp/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The requests won't be automatically matched for cors preflight requests unless e
CORS policies can be specified per route via [RouteConfig.CorsPolicy](xref:Yarp.ReverseProxy.Configuration.RouteConfig) and can be bound from the `Routes` sections of the config file. As with other route properties, this can be modified and reloaded without restarting the proxy. Policy names are case insensitive.

Example:
```JSON
```json
{
"ReverseProxy": {
"Routes": {
Expand Down
24 changes: 12 additions & 12 deletions aspnetcore/fundamentals/servers/yarp/dests-health-checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ YARP can proactively monitor destination health by sending periodic probing requ
There are several cluster-wide configuration settings controlling active health checks that can be set either in the config file or in code. A dedicated health endpoint can also be specified per destination.

#### File example
```JSON
```json
"Clusters": {
"cluster1": {
"HealthCheck": {
Expand Down Expand Up @@ -49,7 +49,7 @@ There are several cluster-wide configuration settings controlling active health
```

#### Code example
```C#
```csharp
var clusters = new[]
{
new ClusterConfig()
Expand Down Expand Up @@ -132,7 +132,7 @@ There are 2 main extensibility points in the active health check subsystem.

The below is a simple example of a custom `IActiveHealthCheckPolicy` marking destination as `Healthy`, if a successful response code was returned for a probe, and as `Unhealthy` otherwise.

```C#
```csharp
public class FirstUnsuccessfulResponseHealthPolicy : IActiveHealthCheckPolicy
{
private readonly IDestinationHealthUpdater _healthUpdater;
Expand Down Expand Up @@ -169,13 +169,13 @@ public class FirstUnsuccessfulResponseHealthPolicy : IActiveHealthCheckPolicy

The default `IProbingRequestFactory` uses the same `HttpRequest` configuration as proxy requests, to customize that implement your own `IProbingRequestFactory` and register it in DI like the below.

```C#
```csharp
services.AddSingleton<IProbingRequestFactory, CustomProbingRequestFactory>();
```

The below is a simple example of a customer `IProbingRequestFactory` concatenating `DestinationConfig.Address` and a fixed health probe path to create the probing request URI.

```C#
```csharp
public class CustomProbingRequestFactory : IProbingRequestFactory
{
public HttpRequestMessage CreateRequest(ClusterConfig clusterConfig, DestinationConfig destinationConfig)
Expand All @@ -196,7 +196,7 @@ There is one important difference from the active health check logic. Once a des
There are several cluster-wide configuration settings controlling passive health checks that can be set either in the config file or in code.

#### File example
```JSON
```json
"Clusters": {
"cluster1": {
"HealthCheck": {
Expand All @@ -222,7 +222,7 @@ There are several cluster-wide configuration settings controlling passive health
```

#### Code example
```C#
```csharp
var clusters = new[]
{
new ClusterConfig()
Expand Down Expand Up @@ -252,7 +252,7 @@ Passive health check settings are specified on the cluster level in `Cluster/Hea

Passive health checks require the `PassiveHealthCheckMiddleware` added into the pipeline for them to work. The default `MapReverseProxy(this IEndpointRouteBuilder endpoints)` method does it automatically, but in case of a manual pipeline construction the [UsePassiveHealthChecks](xref:Microsoft.AspNetCore.Builder.AppBuilderHealthExtensions) method must be called to add that middleware as shown in the example below.

```C#
```csharp
endpoints.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.UseAffinitizedDestinationLookup();
Expand All @@ -279,7 +279,7 @@ Global parameters are set via the options mechanism using `TransportFailureRateH
- `DefaultFailureRateLimit` - default failure rate limit for a destination to be marked as unhealthy that is applied if it's not set on a cluster's metadata. The value is in range `(0,1)`. Default is `0.3` (30%).

Global policy options can be set in code as follows:
```C#
```csharp
services.Configure<TransportFailureRateHealthPolicyOptions>(o =>
{
o.DetectionWindowSize = TimeSpan.FromSeconds(30);
Expand Down Expand Up @@ -318,7 +318,7 @@ There is one main extensibility point in the passive health check subsystem, the

The below is a simple example of a custom `IPassiveHealthCheckPolicy` marking destination as `Unhealthy` on the first unsuccessful response to a proxied request.

```C#
```csharp
public class FirstUnsuccessfulResponseHealthPolicy : IPassiveHealthCheckPolicy
{
private static readonly TimeSpan _defaultReactivationPeriod = TimeSpan.FromSeconds(60);
Expand Down Expand Up @@ -355,7 +355,7 @@ Destinations health state is used to determine which of them are eligible for re

### Configuration
#### File example
```JSON
```json
"Clusters": {
"cluster1": {
"HealthCheck": {
Expand All @@ -377,7 +377,7 @@ Destinations health state is used to determine which of them are eligible for re
```

#### Code example
```C#
```csharp
var clusters = new[]
{
new ClusterConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The [Metrics sample](https://github.com/microsoft/reverse-proxy/tree/main/sample

To use either of these, create a class implementing a [`Yarp.Telemetry.Consumption` interface](xref:Yarp.Telemetry.Consumption#interfaces), such as <xref:Yarp.Telemetry.Consumption.IForwarderTelemetryConsumer>:

```C#
```csharp
public class ForwarderTelemetry : IForwarderTelemetryConsumer
{
/// Called before forwarding a request.
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/fundamentals/servers/yarp/direct-forwarding.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ In this example the IHttpForwarder is registered in DI, injected into the endpoi

The optional transforms show how to copy all request headers except for the `Host`, it's common that the destination requires its own `Host` from the url.

```C#
```csharp
using System;
using System.Diagnostics;
using System.Net;
Expand Down Expand Up @@ -136,7 +136,7 @@ internal class CustomTransformer : HttpTransformer

There are also [extension methods](xref:Microsoft.AspNetCore.Builder.DirectForwardingIEndpointRouteBuilderExtensions) available that simplify the mapping of IHttpForwarder to endpoints.

```C#
```csharp
app.MapForwarder("/{**catch-all}", "https://localhost:10000/", requestConfig, transformer, httpClient);
```

Expand Down
10 changes: 5 additions & 5 deletions aspnetcore/fundamentals/servers/yarp/distributed-tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ For example, to monitor the traces with Application Insights, the proxy applicat

`application.csproj`:

``` xml
```xml
<ItemGroup>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.3" />
</ItemGroup>
```

`Program.cs`:

``` c#
```csharp
using Azure.Monitor.OpenTelemetry.AspNetCore;
using OpenTelemetry.Trace;
using System.Diagnostics;
Expand Down Expand Up @@ -70,7 +70,7 @@ app.Run();

### Example: OpenTelemetry hosting

``` xml
```xml
<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
Expand All @@ -81,7 +81,7 @@ app.Run();
</ItemGroup>
```

``` csharp
```csharp
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
Expand Down Expand Up @@ -136,7 +136,7 @@ YARP will remove any header in [`DistributedContextPropagator.Fields`] so that t

If you do not wish the proxy to actively participate in the trace, and wish to keep all the tracing headers as-is, you may do so by setting `SocketsHttpHandler.ActivityHeadersPropagator` to `null`.

```c#
```csharp
services.AddReverseProxy()
.ConfigureHttpClient((context, handler) => handler.ActivityHeadersPropagator = null);
```
Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/fundamentals/servers/yarp/header-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Headers are specified in the `Match` section of a proxy route.
If multiple headers rules are specified on a route then all must match for a route to be taken. OR logic must be implemented either within a header rule or as separate routes.

Configuration:
```JSON
```json
"Routes": {
"route1" : {
"ClusterId": "cluster1",
Expand Down Expand Up @@ -140,7 +140,7 @@ Configuration:
```

Code:
```C#
```csharp
var routes = new[]
{
new RouteConfig()
Expand Down
Loading