.NET Library Documentation

HttpLifetimeHeaderValueParser

Namespace: System.Net.Http

Parses the HttpLifecycleHeaderValue values. This class is used internally by the .NET framework and is not intended for direct use by application code. It provides mechanisms for parsing and validating specific HTTP header formats related to lifecycle management.

Remarks

The HttpLifetimeHeaderValueParser is a low-level component designed to handle the intricacies of parsing header values that define the lifecycle or duration of certain HTTP-related configurations or states. These headers might include directives for caching, connection pooling, or request timeouts. Due to its internal nature, direct manipulation or reliance on this class is discouraged as its implementation details may change without notice across .NET versions.

Methods

Method Description
static HttpLifecycleHeaderValue Parse(ReadOnlySpan<char> input) Parses the input string into an HttpLifecycleHeaderValue object.
Parameters:
  • input: A read-only span of characters representing the header value to parse.
Returns: An HttpLifecycleHeaderValue object representing the parsed header value. Exceptions: FormatException: If the input string is not a valid HttpLifecycleHeaderValue.
static bool TryParse(ReadOnlySpan<char> input, out HttpLifecycleHeaderValue value) Attempts to parse the input string into an HttpLifecycleHeaderValue object without throwing an exception.
Parameters:
  • input: A read-only span of characters representing the header value to parse.
  • value: When this method returns, contains the parsed HttpLifecycleHeaderValue object if the parsing succeeded, or a default value if the parsing failed.
Returns: true if the input string was successfully parsed; otherwise, false.

Example Usage (Illustrative)

While direct usage is discouraged, here's how the Parse method might be used internally or in hypothetical scenarios for understanding:


using System.Net.Http.Headers;

// Example 1: Parsing a valid header value
string headerValue1 = "max-age=3600, proxy-revalidate";
if (HttpLifetimeHeaderValueParser.TryParse(headerValue1.AsSpan(), out var parsedValue1))
{
    Console.WriteLine($"Successfully parsed: {parsedValue1}");
    // Access properties of parsedValue1 if needed (e.g., parsedValue1.MaxAge)
}
else
{
    Console.WriteLine("Failed to parse header value 1.");
}

// Example 2: Parsing a header value with an invalid format
string headerValue2 = "invalid-directive=abc";
if (HttpLifetimeHeaderValueParser.TryParse(headerValue2.AsSpan(), out var parsedValue2))
{
    Console.WriteLine($"Successfully parsed: {parsedValue2}");
}
else
{
    Console.WriteLine("Failed to parse header value 2."); // This is expected
}

// Example 3: Using Parse and catching exceptions
try
{
    string headerValue3 = "public, must-revalidate, max-age=0";
    HttpLifecycleHeaderValue parsedValue3 = HttpLifetimeHeaderValueParser.Parse(headerValue3.AsSpan());
    Console.WriteLine($"Parsed value: {parsedValue3}");
}
catch (FormatException ex)
{
    Console.WriteLine($"Error parsing header: {ex.Message}");
}