Provides extensions for service discovery for the YARP reverse proxy.
$ dotnet add package Microsoft.Extensions.ServiceDiscovery.YarpThe Microsoft.Extensions.ServiceDiscovery.Yarp library adds support for resolving endpoints for YARP clusters, by implementing a YARP destination resolver.
The IReverseProxyBuilder.AddServiceDiscoveryDestinationResolver() extension method configures a YARP destination resolver. To use this method, you must also configure YARP itself as described in the YARP documentation, and you must configure .NET Service Discovery via the Microsoft.Extensions.ServiceDiscovery library.
IHttpForwarderYARP supports direct forwarding of specific requests using the IHttpForwarder interface. This, too, can benefit from service discovery using the Microsoft.Extensions.ServiceDiscovery library. To take advantage of service discovery when using YARP Direct Forwarding, use the IServiceCollection.AddHttpForwarderWithServiceDiscovery method.
For example, consider the following .NET Aspire application:
var builder = WebApplication.CreateBuilder(args);
// Configure service discovery
builder.Services.AddServiceDiscovery();
// Add YARP Direct Forwarding with Service Discovery support
builder.Services.AddHttpForwarderWithServiceDiscovery();
// ... other configuration ...
var app = builder.Build();
// ... other configuration ...
// Map a Direct Forwarder which forwards requests to the resolved "catalogservice" endpoints
app.MapForwarder("/catalog/images/{id}", "http://catalogservice", "/api/v1/catalog/items/{id}/image");
app.Run();
In the above example, the YARP Direct Forwarder will resolve the catalogservice using service discovery, forwarding request sent to the /catalog/images/{id} endpoint to the destination path on the resolved endpoints.