You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/servers/yarp/extensibility-transforms.md
+66-41Lines changed: 66 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,6 @@ ms.topic: article
9
9
content_well_notification: AI-contribution
10
10
ai-usage: ai-assisted
11
11
---
12
-
13
12
# Request and Response Transform Extensibility
14
13
15
14
## Introduction
@@ -23,23 +22,23 @@ All request transforms must derive from the abstract base class [RequestTransfor
23
22
24
23
A request transform may conditionally produce an immediate response such as for error conditions. This prevents any remaining transforms from running and the request from being proxied. This is indicated by setting the `HttpResponse.StatusCode` to a value other than 200, or calling `HttpResponse.StartAsync()`, or writing to the `HttpResponse.Body` or `BodyWriter`.
25
24
26
-
### AddRequestTransform
25
+
### `AddRequestTransform`
27
26
28
27
[AddRequestTransform](xref:Yarp.ReverseProxy.Transforms.TransformBuilderContextFuncExtensions.AddRequestTransform*) is a `TransformBuilderContext` extension method that defines a request transform as a `Func<RequestTransformContext, ValueTask>`. This allows creating a custom request transform without implementing a `RequestTransform` derived class.
29
28
30
-
## ResponseTransform
29
+
## `ResponseTransform`
31
30
32
31
All response transforms must derive from the abstract base class [ResponseTransform](xref:Yarp.ReverseProxy.Transforms.ResponseTransform). These can freely modify the client `HttpResponse`. Avoid reading or modifying the response body as this may disrupt the proxying flow. Consider also adding a parametrized extension method on `TransformBuilderContext` for discoverability and easy of use.
33
32
34
-
### AddResponseTransform
33
+
### `AddResponseTransform`
35
34
36
35
[AddResponseTransform](xref:Yarp.ReverseProxy.Transforms.TransformBuilderContextFuncExtensions.AddResponseTransform*) is a `TransformBuilderContext` extension method that defines a response transform as a `Func<ResponseTransformContext, ValueTask>`. This allows creating a custom response transform without implementing a `ResponseTransform` derived class.
37
36
38
-
## ResponseTrailersTransform
37
+
## `ResponseTrailersTransform`
39
38
40
39
All response trailers transforms must derive from the abstract base class [ResponseTrailersTransform](xref:Yarp.ReverseProxy.Transforms.ResponseTrailersTransform). These can freely modify the client HttpResponse trailers. These run after the response body and should not attempt to modify the response headers or body. Consider also adding a parametrized extension method on `TransformBuilderContext` for discoverability and easy of use.
41
40
42
-
### AddResponseTrailersTransform
41
+
### `AddResponseTrailersTransform`
43
42
44
43
[AddResponseTrailersTransform](xref:Yarp.ReverseProxy.Transforms.TransformBuilderContextFuncExtensions.AddResponseTrailersTransform*) is a `TransformBuilderContext` extension method that defines a response trailers transform as a `Func<ResponseTrailersTransformContext, ValueTask>`. This allows creating a custom response trailers transform without implementing a `ResponseTrailersTransform` derived class.
45
44
@@ -53,22 +52,25 @@ The below example uses simple, inefficient buffering to transform requests. A mo
53
52
54
53
This sample requires YARP 1.1, see https://github.com/microsoft/reverse-proxy/pull/1569.
@@ -82,12 +84,13 @@ Be careful about which kinds of responses are modified, how much data gets buffe
82
84
83
85
The below example uses simple, inefficient buffering to transform responses. A more efficient implementation would wrap the stream returned by `ReadAsStreamAsync()` with a stream that performed the needed modifications as data was proxied from client to server. That would also require removing the Content-Length header since the final length would not be known in advance.
[ITransformProvider](xref:Yarp.ReverseProxy.Transforms.Builder.ITransformProvider) provides the functionality of `AddTransforms` described above as well as DI integration and validation support.
116
+
<xref:Yarp.ReverseProxy.Transforms.Builder.ITransformProvider> provides the functionality of `AddTransforms` described above as well as DI integration and validation support.
113
117
114
118
`ITransformProvider`'s can be registered in DI by calling [AddTransforms<T>()](xref:Microsoft.Extensions.DependencyInjection.ReverseProxyServiceCollectionExtensions). Multiple `ITransformProvider` implementations can be registered and all will be run.
115
119
116
120
`ITransformProvider` has two methods, `Validate` and `Apply`. `Validate` gives you the opportunity to inspect the route for any parameters that are needed to configure a transform, such as custom metadata, and to return validation errors on the context if any needed values are missing or invalid. The `Apply` method provides the same functionality as AddTransform as discussed above, adding and configuring transforms per route.
Developers that want to integrate their custom transforms with the `Transforms` section of configuration can implement an [ITransformFactory](xref:Yarp.ReverseProxy.Transforms.Builder.ITransformFactory). This should be registered in DI using the `AddTransformFactory<T>()` method. Multiple factories can be registered and all will be used.
174
190
@@ -178,40 +194,47 @@ The `Validate` method is called when loading a configuration to verify the conte
178
194
179
195
The `Build` method takes the given configuration and produces the associated transform instances for the route.
0 commit comments