Yarp-based Direct Forwarding Pattern

satish1v
2 min readOct 21, 2022
Photo by Joe Gardner on Unsplash

A reverse proxy can be used to spread out the work, make the system more secure, send traffic to a different upstream, and so on. However, in some cases, we only need to forward the request to the external system without making any changes. The direct forwarding pattern comes into play here.

General Use cases for this pattern

  1. Large File Upload or Download from the external application
  2. Dynamic Host selection — Based on Application logic the Host can differ

Since Yarp is a library for making a reverse proxy, any custom logic falls under the basic usage. So lets start with a Simple minimal dotnet6 Empty template

Following steps make the pattern possible

  1. Add a AddHttpForwarder() to existing service. This adds the HTTPForwarder to the application
  2. Line number 13 adds the HTTP Client and this is totally optional step but I have a provided the best practices for configuring the use case
  3. Based on the Route defined, In this “/google/{{**catch-all}}” we have used the forwarder to Send the request to the external system.

This pattern comes in handy when dealing with an external service that does the heavy lifting. For example, use the pattern if you want to show a file you downloaded from AWS S3 without using a pre-signed URL.

--

--