Skip to content

Commit 815b4f8

Browse files
Add a snippet about header propogation (dotnet#16705)
* Add a snippet about header propogation Co-authored-by: Rick Anderson <[email protected]>
2 parents 73be7bd + 5ebd26b commit 815b4f8

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

aspnetcore/fundamentals/http-requests.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,22 @@ In the following example:
347347

348348
[!code-csharp[](http-requests/samples/3.x/HttpClientFactoryConsoleSample/Program.cs?highlight=14-15,20,26-27,59-62)]
349349

350+
## Header propagation middleware
351+
352+
Header propagation is an ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests. To use header propagation:
353+
354+
* Reference the [Microsoft.AspNetCore.HeaderPropagation](https://www.nuget.org/packages/Microsoft.AspNetCore.HeaderPropagation) package.
355+
* Configure the middleware and `HttpClient` in `Startup`:
356+
357+
[!code-csharp[](http-requests/samples/3.x/Startup.cs?highlight=5-9,21&name=snippet)]
358+
359+
* The client includes the configured headers on outbound requests:
360+
361+
```C#
362+
var client = clientFactory.CreateClient("MyForwardingClient");
363+
var response = client.GetAsync(...);
364+
```
365+
350366
## Additional resources
351367

352368
* [Use HttpClientFactory to implement resilient HTTP requests](/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests)
@@ -957,6 +973,23 @@ In the following example:
957973

958974
[!code-csharp[](http-requests/samples/2.x/HttpClientFactoryConsoleSample/Program.cs?highlight=14-15,20,26-27,59-62)]
959975

976+
## Header propagation middleware
977+
978+
Header propagation is a community supported middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests. To use header propagation:
979+
980+
* Reference the community supported port of the package [HeaderPropagation](https://www.nuget.org/packages/HeaderPropagation). ASP.NET Core 3.1 and later supports [Microsoft.AspNetCore.HeaderPropagation](https://www.nuget.org/packages/Microsoft.AspNetCore.HeaderPropagation).
981+
982+
* Configure the middleware and `HttpClient` in `Startup`:
983+
984+
[!code-csharp[](http-requests/samples/2.x/Startup21.cs?highlight=5-9,25&name=snippet)]
985+
986+
* The client includes the configured headers on outbound requests:
987+
988+
```C#
989+
var client = clientFactory.CreateClient("MyForwardingClient");
990+
var response = client.GetAsync(...);
991+
```
992+
960993
## Additional resources
961994

962995
* [Use HttpClientFactory to implement resilient HTTP requests](/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace HeaderProp21
8+
{
9+
public class Startup21
10+
{
11+
public Startup21(IConfiguration configuration)
12+
{
13+
Configuration = configuration;
14+
}
15+
16+
public IConfiguration Configuration { get; }
17+
18+
#region snippet
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
22+
23+
services.AddHttpClient("MyForwardingClient").AddHeaderPropagation();
24+
services.AddHeaderPropagation(options =>
25+
{
26+
options.Headers.Add("X-TraceId");
27+
});
28+
}
29+
30+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
31+
{
32+
if (env.IsDevelopment())
33+
{
34+
app.UseDeveloperExceptionPage();
35+
}
36+
else
37+
{
38+
app.UseHsts();
39+
}
40+
41+
app.UseHttpsRedirection();
42+
43+
app.UseHeaderPropagation();
44+
45+
app.UseMvc();
46+
}
47+
#endregion
48+
}
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
7+
namespace HeaderProp
8+
{
9+
public class Startup
10+
{
11+
public Startup(IConfiguration configuration)
12+
{
13+
Configuration = configuration;
14+
}
15+
16+
public IConfiguration Configuration { get; }
17+
18+
#region snippet
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddControllers();
22+
23+
services.AddHttpClient("MyForwardingClient").AddHeaderPropagation();
24+
services.AddHeaderPropagation(options =>
25+
{
26+
options.Headers.Add("X-TraceId");
27+
});
28+
}
29+
30+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
31+
{
32+
if (env.IsDevelopment())
33+
{
34+
app.UseDeveloperExceptionPage();
35+
}
36+
37+
app.UseHttpsRedirection();
38+
39+
app.UseHeaderPropagation();
40+
41+
app.UseRouting();
42+
43+
app.UseAuthorization();
44+
45+
app.UseEndpoints(endpoints =>
46+
{
47+
endpoints.MapControllers();
48+
});
49+
}
50+
#endregion
51+
}
52+
}

0 commit comments

Comments
 (0)