All Projects → alefranz → Headerpropagation

alefranz / Headerpropagation

Licence: apache-2.0
ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests. This is a backport to ASP.NET Core 2.1 (and 2.2) of the ASP.NET Core HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.

Projects that are alternatives of or similar to Headerpropagation

Csla
A home for your business logic in any .NET application.
Stars: ✭ 865 (+3660.87%)
Mutual labels:  aspnetcore, netstandard
Elmahcore
ELMAH for Net.Standard and Net.Core
Stars: ✭ 127 (+452.17%)
Mutual labels:  aspnetcore, netstandard
Httpclient Interception
A .NET Standard library for intercepting server-side HTTP dependencies
Stars: ✭ 108 (+369.57%)
Mutual labels:  netstandard, httpclient
Fluentlyhttpclient
Http Client for .NET Standard with fluent APIs which are intuitive, easy to use and also highly extensible.
Stars: ✭ 73 (+217.39%)
Mutual labels:  netstandard, httpclient
profiler-api
The portable version of JetBrains profiler API for .NET Framework / .NET Core / .NET / .NET Standard / Mono
Stars: ✭ 21 (-8.7%)
Mutual labels:  aspnetcore, netstandard
Osharp
OSharp是一个基于.NetCore的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net Core 框架更易于应用到实际项目开发中。
Stars: ✭ 2,151 (+9252.17%)
Mutual labels:  aspnetcore, netstandard
Couchdb Net
EF Core-like CouchDB experience for .NET!
Stars: ✭ 50 (+117.39%)
Mutual labels:  aspnetcore, netstandard
AspNetCore.Client
On Build client generator for asp.net core projects
Stars: ✭ 14 (-39.13%)
Mutual labels:  aspnetcore, httpclient
aspnetcore-authentication-apikey
Easy to use and very light weight Microsoft style API Key Authentication Implementation for ASP.NET Core. It can be setup so that it can accept API Key in Header, Authorization Header, QueryParams or HeaderOrQueryParams.
Stars: ✭ 215 (+834.78%)
Mutual labels:  aspnetcore, netstandard
Revo
Event Sourcing, CQRS and DDD framework for C#/.NET Core.
Stars: ✭ 162 (+604.35%)
Mutual labels:  aspnetcore, netstandard
IGeekFan.AspNetCore.Knife4jUI
support .NET Core3.0+,.NET Standard2.0 Swagger UI knife4j ui,you can use NSwagger or Swashbuckle.AspNetCore in packages
Stars: ✭ 178 (+673.91%)
Mutual labels:  aspnetcore, netstandard
Cosmos.Identity
A Cosmos storage provider for ASP.NET Core Identity.
Stars: ✭ 26 (+13.04%)
Mutual labels:  aspnetcore, netstandard
Xamarinmediamanager
Cross platform Xamarin plugin to play and control Audio and Video
Stars: ✭ 647 (+2713.04%)
Mutual labels:  netstandard
Aspnetcore Developer Roadmap
Roadmap to becoming an ASP.NET Core developer in 2021
Stars: ✭ 8,248 (+35760.87%)
Mutual labels:  aspnetcore
Practical Aspnetcore
Practical samples of ASP.NET Core 2.1, 2.2, 3.1, 5.0 and 6.0 projects you can use. Readme contains explanations on all projects.
Stars: ✭ 6,199 (+26852.17%)
Mutual labels:  aspnetcore
Smartformat
An extensible .NET replacement for String.Format
Stars: ✭ 642 (+2691.3%)
Mutual labels:  netstandard
Gout
gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
Stars: ✭ 749 (+3156.52%)
Mutual labels:  httpclient
Mtrans
Multi-source Translation
Stars: ✭ 711 (+2991.3%)
Mutual labels:  httpclient
Mockhttp
Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API.
Stars: ✭ 623 (+2608.7%)
Mutual labels:  httpclient
Cleanarchitecture.webapi
An implementation of Clean Architecture for ASP.NET Core 3.1 WebAPI. Built with loosely coupled architecture and clean-code practices in mind.
Stars: ✭ 615 (+2573.91%)
Mutual labels:  aspnetcore

Build Status

About HeaderPropagation

This is a backport to ASP.NET Core 2.1 (and 2.2) of the HeaderPropagation middleware I had recently contributed to the ASP.NET Core project. All code is licensed under the Apache License, Version 2.0 and copyrighted by the .NET Foundation.

If you are using ASP.NET Core 3.0, please use the official package Microsoft.AspNetCore.HeaderPropagation.

Motivation

I believe it is a common use case which deserves to be included in ASP.NET Core. Its main use case is probably to track distributed transaction which requires the ability to pass through a transaction identifier as well as generating a new one when not present.

Given the ASP.NET Core 3.0 release is quite far away, and the current policy doesn't allow to backport new features to already shipped releases, I have created this package as recommended so it can be used today on projects based on ASP.NET Core 2.1 or 2.2.

Usage

In Startup.Configure enable the middleware:

app.UseHeaderPropagation();

In Startup.ConfigureServices add the required services, eventually specifying a configuration action:

services.AddHeaderPropagation(o =>
{
    // propagate the header if present
    o.Headers.Add("User-Agent");

    // if still missing, set it with a value factory
    o.Headers.Add("User-Agent", context => "Mozilla/5.0 (trust me, I'm really Mozilla!)");

    // propagate the header if present, using a different name in the outbound request
    o.Headers.Add("Accept-Language", "Lang");
});

If you are using the HttpClientFactory, add the DelegatingHandler to the client configuration using the AddHeaderPropagation extension method.

services.AddHttpClient<GitHubClient>(c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
}).AddHeaderPropagation();

Or propagate only a specific header, also redefining the name to use

services.AddHttpClient("example", c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
}).AddHeaderPropagation(o =>
{
    o.Headers.Add("User-Agent", "Source");
});

See samples/WebApplication.

Behaviour

HeaderPropagationOptions contains a dictionary where the key represent the name of the header to consume from the incoming request.

Each entry define the behaviour to propagate that header as follows:

  • InboundHeaderName is the name of the header to be captured.
  • CapturedHeaderName determines the name of the header to be used by default for the outbound http requests. If not specified, defaults to InboundHeaderName.
  • When present, the ValueFilter delegate will be evaluated once per request to provide the transformed header value. The delegate will be called regardless of whether a header with the name corresponding to InboundHeaderName is present in the request. It should return StringValues.Empty to not add the header.
  • If multiple configurations for the same header are present, the first which returns a value wins.

Please note the factory is called only once per incoming request and the same value will be used by all the outbound calls.

HeaderPropagationMessageHandlerOptions allows to customize the behaviour per clients, where each entry define the behaviour as follows:

  • CapturedHeaderName is the name of the header to be used to lookup the headers captured.
  • OutboundHeaderName is the name of the header to be added to the outgoing http requests. If not specified, defaults to CapturedHeaderName.

Acknowledgements

This feature would not have been possible without the help of @rynowak who helped to refine it and get it merged into ASP.NET Core.

You can find the list of contributions in the original repository.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].