{"id":38147032,"url":"https://github.com/fkucukkara/delegatinghandler-playground","last_synced_at":"2026-01-16T22:57:27.569Z","repository":{"id":331225387,"uuid":"1125785244","full_name":"fkucukkara/delegatinghandler-playground","owner":"fkucukkara","description":"This playground demonstrates how to use custom DelegatingHandler in ASP.NET Core Minimal APIs with Refit.","archived":false,"fork":false,"pushed_at":"2026-01-01T12:22:41.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-04T16:32:59.691Z","etag":null,"topics":["delegatinghandler","minimal-api","net10","playground-project","refit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fkucukkara.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-31T11:02:54.000Z","updated_at":"2026-01-01T12:22:44.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/fkucukkara/delegatinghandler-playground","commit_stats":null,"previous_names":["fkucukkara/delegatinghandler-playground"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/fkucukkara/delegatinghandler-playground","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2Fdelegatinghandler-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2Fdelegatinghandler-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2Fdelegatinghandler-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2Fdelegatinghandler-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fkucukkara","download_url":"https://codeload.github.com/fkucukkara/delegatinghandler-playground/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2Fdelegatinghandler-playground/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28486964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T22:54:02.790Z","status":"ssl_error","status_checked_at":"2026-01-16T22:50:10.344Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["delegatinghandler","minimal-api","net10","playground-project","refit"],"created_at":"2026-01-16T22:57:26.961Z","updated_at":"2026-01-16T22:57:27.564Z","avatar_url":"https://github.com/fkucukkara.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DelegatingHandler Playground\n\nThis playground demonstrates how to use custom `DelegatingHandler` in ASP.NET Core Minimal APIs (.NET 10, C# 14). It showcases building a pipeline of HTTP message handlers for cross-cutting concerns like logging, authentication, error handling, and header propagation.\n\n## Main Feature\n- **Custom DelegatingHandlers**: Create and chain multiple `DelegatingHandler` classes to intercept and modify outgoing HTTP requests and responses, enabling features like structured logging with ILogger, API key injection, performance tracking, and comprehensive error handling.\n\n## Side Features\n- **Refit**: Generate strongly-typed HTTP clients from REST API interfaces for clean, type-safe API calls.\n- **Header Propagation Middleware**: Automatically propagate specified headers (e.g., `X-TraceId`) from incoming requests to outgoing HTTP requests using ASP.NET Core's built-in middleware.\n- **Health Checks**: Built-in `/health` endpoint for monitoring application status.\n- **Comprehensive Error Handling**: Proper exception handling with detailed error responses and logging.\n- **Structured Logging**: Uses ASP.NET Core's ILogger for production-ready logging with performance metrics.\n\n## Key Concepts\n### Custom DelegatingHandlers\n- Inherit from `DelegatingHandler` to create handlers for specific concerns (e.g., structured logging with ILogger, adding authentication, performance tracking).\n- Chain handlers in the desired order using `.AddHttpMessageHandler\u003cT\u003e()` on the `HttpClient` builder. Note that built-in handlers like `AddHeaderPropagation()` should be added before custom handlers to ensure proper execution order.\n- Handlers execute in the order they are added, allowing for modular and reusable HTTP pipeline logic.\n- **Best Practices**:\n  - Use dependency injection for `ILogger` and `IConfiguration`.\n  - Validate required configuration (e.g., API keys) and throw meaningful exceptions.\n  - Track request duration for performance monitoring.\n  - Avoid logging sensitive data (e.g., Authorization headers).\n\n### Refit Client\n- Define an interface for your API (see `IWeatherApi`).\n- Register with `AddRefitClient` in DI for automatic client generation.\n- Use in endpoints via dependency injection for seamless API integration.\n\n### Header Propagation Middleware\n- Configure which headers to propagate in `Program.cs` using `AddHeaderPropagation`.\n- Attach to `HttpClient` instances with `.AddHeaderPropagation()`.\n- Use `UseHeaderPropagation()` in the middleware pipeline to enable propagation from incoming requests.\n\n## Example Endpoints\n- `/weather/{city}`: Retrieves current weather for a city using Refit, with handlers chained in this order: Header Propagation (adds propagated headers), Logging (logs request details including headers and performance metrics), and API Key (injects and validates the API key as a query parameter). Includes comprehensive error handling with proper HTTP status codes and structured logging.\n- `/health`: Health check endpoint for monitoring application status.\n\n## Prerequisites\n- Create an account at [WeatherAPI](https://www.weatherapi.com/) to obtain an API key.\n- Set the API key in user-secrets: `dotnet user-secrets set \"WeatherApi:Key\" \"your-api-key\"`\n\n## How to Run\n1. Clone the repo.\n2. Set your weather API key using user-secrets as described above.\n3. Run the project (`dotnet run`).\n4. Explore endpoints with Swagger UI (`/swagger`) or use the health check endpoint (`/health`).\n\n## Troubleshooting\n- **Missing API Key Error**: If you see an error about missing API key, ensure you've set it using: `dotnet user-secrets set \"WeatherApi:Key\" \"your-api-key\"`\n- **404 Not Found**: The city name may be invalid or not found in the WeatherAPI database. Try common city names like \"London\" or \"New York\".\n- **Health Check**: Visit `/health` to verify the application is running correctly.\n\n## Production-Ready Features\n- ✅ Structured logging with ILogger\n- ✅ Comprehensive error handling with proper HTTP status codes\n- ✅ Request/Response validation\n- ✅ Configuration validation (API key required)\n- ✅ Performance tracking (request duration)\n- ✅ Health checks endpoint\n- ✅ Security best practices (no sensitive data in logs)\n- ✅ OpenAPI/Swagger documentation\n\n## References\n- [ASP.NET Core HTTP Requests](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-10.0)\n- [Header Propagation Middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-10.0#header-propagation-middleware)\n- [Minimal APIs Tutorial](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-10.0)\n- [Refit Documentation](https://github.com/reactiveui/refit)\n\n## License\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n**.NET 10 / C# 14** | Modern ASP.NET Core | Educational Playground\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Fdelegatinghandler-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffkucukkara%2Fdelegatinghandler-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Fdelegatinghandler-playground/lists"}