Created
May 24, 2021 19:39
-
-
Save manoj-choudhari-git/1f46734c16432749e8561ea4b43a2c2c to your computer and use it in GitHub Desktop.
.NET Core Web Application - Request Header Propagation
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
// Startup for .NET Core application | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Common Propagation Configuration | |
services.AddHeaderPropagation(options => | |
{ | |
// Propagate X-INBOUND-HEADER if it is present | |
options.Headers.Add("X-INBOUND-HEADER"); | |
// Propagate X-HEADER with its value same as value of X-CUSTOM-HEADER | |
options.Headers.Add("user-agent", "X-RENAMED-HEADER"); | |
// X-CUSTOM-HEADER and its value is can be computed | |
options.Headers.Add("X-CUSTOM-HEADER", context => | |
{ | |
return new StringValues("SomeValue"); | |
}); | |
}); | |
services.AddHttpClient<GitHubApionsumer>(c => | |
{ | |
c.BaseAddress = new Uri("https://api.github.com/"); | |
// Github API versioning | |
c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); | |
// Github requires a user-agent | |
c.DefaultRequestHeaders.Add("User-Agent", "theCodeBlogger.com Demo"); | |
}).AddHeaderPropagation(); // Apply Propagation to Typed Client | |
services.AddControllersWithViews(); | |
} | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
// Add middleware in request pipeline | |
app.UseHeaderPropagation(); | |
app.UseRouting(); | |
app.UseAuthorization(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllerRoute( | |
name: "default", | |
pattern: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment