---
title: Retrieve and consume context rows
summary: Reads a source application's context rows for the current session, then deletes them so the hand-off is consumed exactly once.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-08-17
library: SAIF.Presentation.Web.MVC
reference_type: package
operation_id: OP-MVC-07
trigger: Called by Receive inbound cross-application context (CheckContext)
---

# Retrieve and consume context rows

Implements the read-then-delete pattern that makes cross-application context hand-off single-use: the destination application reads the source application's rows and immediately deletes them, so a page refresh cannot replay a completed hand-off.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-MVC-07 |
| **Trigger** | Called by [Receive inbound cross-application context](receive-inbound-context.md) |
| **Inputs** | Source app, current app, session ID |
| **Outputs** | Hashtable of context values; source rows deleted |
| **Primary components** | `clsContext.RetrieveContext` |
| **Entry point** | SAIF.Presentation.Web.MVC/clsContext.vb |

## Steps

1. Create a `clsContextRepository` and call `Retrieve(sourceApp, currentApp, sessionID)` — a parameterized `SELECT` over `Context` filtered by `AppName`, `SessionID`, and excluding the current application ([SQL-002](../sql/SQL-002-retrieve-context-rows.md), [Context](../database-tables.md#context)).
2. Copy each row's `ContextName`/`ContextValue` into a hashtable.
3. Call `CtxRep.Delete(sourceApp, sessionID)` so context is consumed exactly once ([SQL-004](../sql/SQL-004-delete-context-rows.md), [BR-MVC-017](../business-rules.md#br-mvc-017-cross-application-context-is-consumed-exactly-once)).
4. Return the hashtable.
   - On failure: repository failures are wrapped as `ApplicationException("Context Repository Retrieve Failed.", ex)` after being traced.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([RetrieveContext]) --> S1[SELECT rows for sourceApp/session, excluding currentApp]
	S1 --> S2[Copy ContextName/ContextValue pairs into hashtable]
	S2 --> S3[DELETE the source app's rows]
	S3 --> EndOk([Return hashtable])
	S1 -.->|Exception| EndErr([Wrap as ApplicationException])
	S3 -.->|Exception| EndErr
```

## Failure and edge-case behavior
- A retrieval or delete failure at any step is wrapped as `ApplicationException("Context Repository Retrieve Failed.", ex)`, preserving the original exception as the inner exception.
- Because the delete always follows the select within the same call, an abandoned navigation that never completes its retrieve simply leaves rows behind rather than corrupting state — no cleanup job for stale rows exists in this repository.**

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [SQL catalog](../sql/index.md)
- [Database tables](../database-tables.md)

> **Note:** Statements marked with `**` could not be confirmed; treat as unknown until verified.

## Sources
- SAIF.Presentation.Web.MVC/clsContext.vb
