So far I’ve always used ProxyKit to create a reverse proxy in ASP.NET Core. But with the announcement of Yarp, it is time to try this alternative…
- I created a new ASP.NET Core “empty” project:
dotnet new web -n ProxyTest -f netcoreapp3.1
The template "ASP.NET Core Empty" was created successfully.Processing post-creation actions...
Running 'dotnet restore' on ProxyTest\ProxyTest.csproj...
Restore completed in 278,54 ms for C:\Projects\test\yarptest\ProxyTest\ProxyTest.csproj.Restore succeeded.
- Next step is to reference the Microsoft.ReverseProxy preview nuget package:
<ItemGroup> <PackageReference Include="Microsoft.ReverseProxy" Version="1.0.0-preview.1.*" /> </ItemGroup>
- Now it is time to update our Startup.cs. This is what I had when using Proxykit:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddProxy(); | |
} | |
// Forward HTTP requests to upstream-server:5001: | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.RunProxy(context => context | |
.ForwardTo("http://upstream-server:5001/") | |
.AddXForwardedHeaders() | |
.Send()); | |
} |
- And here is the updated Startup.cs after switching to Yarp:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddReverseProxy() | |
.LoadFromConfig(Configuration.GetSection("ReverseProxy")); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
app.UseRouting(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapReverseProxy(proxyPipeline => | |
{ | |
proxyPipeline.UseProxyLoadBalancing(); | |
}); | |
}); | |
} |
- In Yarp everything is handled through configuration right now, so the real magic is there:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"AllowedHosts": "*", | |
"ReverseProxy": { | |
"Routes": [ | |
{ | |
"RouteId": "app1", | |
"BackendId": "backend1", | |
"Match": { | |
"Host": "localhost" | |
} | |
} | |
], | |
"Backends": { | |
"backend1": { | |
"Destinations": { | |
"backend1_destination1": { | |
"Address": "http://upstream-server:5001/" | |
} | |
} | |
} | |
} | |
} | |
} |
I'm curious on how this will evolve in the future...