1
1
# Aspire.Hosting.Yarp library
2
2
3
- Provides extension methods and resource definitions for a .NET Aspire AppHost to configure a YARP instance.
3
+ Provides extension methods and resource definitions for a .NET Aspire AppHost to configure a YARP reverse proxy instance.
4
4
5
5
## Getting started
6
6
@@ -12,80 +12,155 @@ In your AppHost project, install the .NET Aspire YARP Hosting library with [NuGe
12
12
dotnet add package Aspire.Hosting.Yarp
13
13
```
14
14
15
- ## Usage example
15
+ ## Usage examples
16
16
17
- Then, in the _ Program.cs_ file of ` AppHost ` , add a YARP resource and provide the configuration file using the following methods:
17
+ ### Programmatic configuration
18
+
19
+ The modern approach uses programmatic configuration with the ` WithConfiguration ` method:
20
+
21
+ ``` csharp
22
+ var builder = DistributedApplication .CreateBuilder (args );
23
+
24
+ var backendService = builder .AddProject <Projects .Backend >(" backend" );
25
+ var frontendService = builder .AddProject <Projects .Frontend >(" frontend" );
26
+
27
+ var gateway = builder .AddYarp (" gateway" )
28
+ .WithConfiguration (yarp =>
29
+ {
30
+ // Add a catch-all route for the frontend
31
+ yarp .AddRoute (frontendService );
32
+
33
+ // Add a route with path prefix for the backend API
34
+ yarp .AddRoute (" /api/{**catch-all}" , backendService )
35
+ .WithTransformPathRemovePrefix (" /api" );
36
+ });
37
+
38
+ var app = builder .Build ();
39
+ await app .RunAsync ();
40
+ ```
41
+
42
+ ### Configuration with external services
43
+
44
+ You can also route to external services:
45
+
46
+ ``` csharp
47
+ var externalApi = builder .AddExternalService (" external-api" , " https://api.example.com" );
48
+
49
+ var gateway = builder .AddYarp (" gateway" )
50
+ .WithConfiguration (yarp =>
51
+ {
52
+ yarp .AddRoute (" /external/{**catch-all}" , externalApi )
53
+ .WithTransformPathRemovePrefix (" /external" );
54
+ });
55
+ ```
56
+ ## Configuration API
57
+
58
+ ### Core methods
59
+
60
+ | Method | Description |
61
+ | --------| -------------|
62
+ | ` AddYarp(string name) ` | Adds a YARP container resource to the application |
63
+ | ` WithConfiguration(Action<IYarpConfigurationBuilder>) ` | Configures YARP programmatically |
64
+ | ` WithHostPort(int? port) ` | Sets a specific host port instead of random assignment |
65
+
66
+ ### Route configuration
67
+
68
+ The ` IYarpConfigurationBuilder ` provides methods to configure routes and clusters:
69
+
70
+ ``` csharp
71
+ // Add routes with different targets
72
+ yarp .AddRoute (resource ); // Catch-all route
73
+ yarp .AddRoute (" /path/{**catch-all}" , resource ); // Specific path route
74
+ yarp .AddRoute (" /path/{**catch-all}" , endpoint ); // Route to specific endpoint
75
+ yarp .AddRoute (" /path/{**catch-all}" , externalService ); // Route to external service
76
+
77
+ // Add clusters directly
78
+ var cluster = yarp .AddCluster (resource );
79
+ var route = yarp .AddRoute (" /path/{**catch-all}" , cluster );
80
+ ```
81
+
82
+ ### Route matching options
83
+
84
+ Routes can be configured with various matching criteria:
85
+
86
+ ``` csharp
87
+ yarp .AddRoute (" /api/{**catch-all}" , backendService )
88
+ .WithMatchMethods (" GET" , " POST" ) // HTTP methods
89
+ .WithMatchHeaders (new RouteHeader (" Content-Type" , " application/json" )) // Headers
90
+ .WithMatchHosts (" api.example.com" ) // Host header
91
+ .WithOrder (1 ); // Route priority
92
+ ```
93
+
94
+ ## Transform extensions
95
+
96
+ YARP provides various transform extensions to modify requests and responses:
97
+
98
+ ### Path transforms
99
+
100
+ ``` csharp
101
+ route .WithTransformPathSet (" /new/path" ) // Set path
102
+ .WithTransformPathPrefix (" /prefix" ) // Add prefix
103
+ .WithTransformPathRemovePrefix (" /api" ) // Remove prefix
104
+ .WithTransformPathRouteValues (" /users/{id}/posts" ); // Use route values
105
+ ```
106
+
107
+ ### Request header transforms
108
+
109
+ ``` csharp
110
+ route .WithTransformRequestHeader (" X-Forwarded-For" , " value" ) // Add/set header
111
+ .WithTransformRequestHeaderRouteValue (" X-User-Id" , " id" ) // From route value
112
+ .WithTransformUseOriginalHostHeader (true ) // Preserve host
113
+ .WithTransformCopyRequestHeaders (false ); // Copy headers
114
+ ```
115
+
116
+ ### Response transforms
18
117
19
118
``` csharp
20
- var catalogService = builder .AddProject <Projects .CatalogService >(" catalogservice" )
21
- [.. .];
22
- var basketService = builder .AddProject <Projects .BasketService >(" basketservice" )
23
- [.. .];
24
-
25
- builder .AddYarp (" apigateway" )
26
- .WithConfigFile (" yarp.json" )
27
- .WithReference (basketService )
28
- .WithReference (catalogService );
119
+ route .WithTransformResponseHeader (" X-Powered-By" , " Aspire" ) // Add response header
120
+ .WithTransformResponseHeaderRemove (" Server" ) // Remove header
121
+ .WithTransformCopyResponseHeaders (true ); // Copy headers
29
122
```
30
123
31
- The ` yarp.json ` configuration file can use the referenced service like this:
32
-
33
- ``` json
34
- {
35
- "Logging" : {
36
- "LogLevel" : {
37
- "Default" : " Information" ,
38
- "Microsoft.AspNetCore" : " Information"
39
- }
40
- },
41
- "AllowedHosts" : " *" ,
42
- "ReverseProxy" : {
43
- "Routes" : {
44
- "catalog" : {
45
- "ClusterId" : " catalog" ,
46
- "Match" : {
47
- "Path" : " /catalog/{**catch-all}"
48
- },
49
- "Transforms" : [
50
- { "PathRemovePrefix" : " /catalog" }
51
- ]
52
- },
53
- "basket" : {
54
- "ClusterId" : " basket" ,
55
- "Match" : {
56
- "Path" : " /basket/{**catch-all}"
57
- },
58
- "Transforms" : [
59
- { "PathRemovePrefix" : " /basket" }
60
- ]
61
- }
62
- },
63
- "Clusters" : {
64
- "catalog" : {
65
- "Destinations" : {
66
- "catalog" : {
67
- "Address" : " http://catalogservice" ,
68
- }
69
- }
70
- },
71
- "basket" : {
72
- "Destinations" : {
73
- "basket" : {
74
- "Address" : " http://basketservice" ,
75
- }
76
- }
77
- }
78
- }
79
- }
80
- }
124
+ ### Query parameter transforms
81
125
126
+ ``` csharp
127
+ route .WithTransformQueryValue (" version" , " 1.0" ) // Add query param
128
+ .WithTransformQueryRouteValue (" userId" , " id" ) // From route value
129
+ .WithTransformQueryRemoveKey (" debug" ); // Remove query param
130
+ ```
131
+
132
+ ## Advanced configuration
133
+
134
+ ### Custom port configuration
135
+
136
+ ``` csharp
137
+ var gateway = builder .AddYarp (" gateway" )
138
+ .WithHostPort (8080 ) // Use specific port
139
+ .WithConfiguration (yarp => { /* config */ });
140
+ ```
141
+
142
+ ### Multiple routes to the same service
143
+
144
+ ``` csharp
145
+ builder .AddYarp (" gateway" )
146
+ .WithConfiguration (yarp =>
147
+ {
148
+ // Different routes to the same backend
149
+ yarp .AddRoute (" /api/v1/{**catch-all}" , backendService )
150
+ .WithTransformPathRemovePrefix (" /api/v1" );
151
+
152
+ yarp .AddRoute (" /api/v2/{**catch-all}" , backendService )
153
+ .WithTransformPathRemovePrefix (" /api/v2" )
154
+ .WithTransformPathPrefix (" /v2" );
155
+ });
82
156
```
83
157
84
158
## Additional documentation
85
159
86
- * https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-component
87
- * https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-output-caching-component
88
- * https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-distributed-caching-component
160
+ * [ YARP documentation] ( https://microsoft.github.io/reverse-proxy/ )
161
+ * [ .NET Aspire documentation] ( https://learn.microsoft.com/dotnet/aspire/ )
162
+ * [ YARP integration in .NET Aspire] ( https://learn.microsoft.com/dotnet/aspire/proxies/yarp-integration )
163
+ * [ Service Discovery in .NET Aspire] ( https://learn.microsoft.com/dotnet/aspire/service-discovery/overview )
89
164
90
165
## Feedback & contributing
91
166
0 commit comments