๐ Request Result Pattern
Rich HTTP responses with correlation IDs, timing metrics, caller information, and comprehensive error tracking
Basic Request with Context
Get a joke with full request context, correlation ID, and timing metrics
Request with Custom Headers
Demonstrates adding custom headers to HTTP requests
๐ฏ HttpRequestResult Features
Context Information
- Correlation ID for request tracking
- Request duration in milliseconds
- Caller member name
- Caller file path
- Caller line number
- Request/Response timestamps
Error Handling
- Comprehensive error list
- HTTP status code
- Success/failure indicators
- Request context dictionary
- Detailed exception information
- Structured logging integration
๐ Code Example
// Create request with full context
var request = new HttpRequestResult<JokeResponse>
{
RequestPath = "https://v2.jokeapi.dev/joke/Programming?safe-mode",
RequestMethod = HttpMethod.Get,
IsDebugEnabled = true, // Enable detailed logging
RequestHeaders = new Dictionary<string, string>
{
["X-Custom-Header"] = "MyValue"
}
};
// Execute request
var result = await _requestResultService.HttpSendRequestResultAsync(request);
// Access rich context
Console.WriteLine($"Correlation ID: {result.CorrelationId}");
Console.WriteLine($"Duration: {result.RequestDurationMilliseconds}ms");
Console.WriteLine($"Called from: {result.CallerMemberName} at line {result.CallerLineNumber}");
Console.WriteLine($"Status: {result.StatusCode}");
if (result.IsSuccessStatusCode)
{
var joke = result.ResponseResults;
Console.WriteLine($"Joke: {joke.FullJoke}");
}
else
{
Console.WriteLine($"Errors: {string.Join(", ", result.ErrorList)}");
}
Why Use HttpRequestResult?
- Debugging: Correlation IDs make tracking requests across logs easy
- Performance Monitoring: Built-in timing helps identify bottlenecks
- Error Handling: Comprehensive error information for better debugging
- Observability: Caller information helps understand request origins
- Context Propagation: Pass additional context through requests
- Testing: Rich result objects make unit testing easier