---
title: ContextParser
summary: API reference for ContextParser.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# ContextParser

Implements `IContextParser`, extracting a numeric policy or agency identifier from an `IWebContext`'s current value and throwing distinct typed exceptions (`NoContextException`, `InvalidContextException`) when the context is missing or not a valid number. It has no injected collaborators and performs no persistence; its two virtual methods are overridable extension points for callers that need to intercept context parsing.

## Purpose
- Parses the policy number carried in the current web context, distinguishing "no context supplied" from "context value is not numeric".
- Parses the agency number carried in the current web context using the same missing-vs-invalid distinction, with agency-specific exception message keys.

## Context
- Namespace: `SAIF.Services.OutputDocuments`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| GetPolicyNumber | `public virtual long GetPolicyNumber(IWebContext context)` | Returns the policy number parsed from `context.CurrentValue`; throws `NoContextException` when the value is null/blank and `InvalidContextException` when it is not a parseable long. |
| GetAgencyNumber | `public virtual long GetAgencyNumber(IWebContext context)` | Returns the agency number parsed from `context.CurrentValue`; throws `NoContextException("NoAgencyContext")` when the value is null/blank and `InvalidContextException("InvalidAgencyContext")` when it is not a parseable long. |

## Behavioral notes
- Both methods are declared `virtual`, allowing derived types to override parsing behavior for testing or specialization*.
- Neither method validates that `context` itself is non-null, relying on the caller to supply a valid `IWebContext` instance.

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts. Statements
> marked with ** could not be confirmed; treat as unknown until verified.

## Sources
- Services/SAIF.Services/OutputDocuments/ContextParser.cs
