---
title: Initialize the web context service for a session
summary: Set up the session-backed context dictionary and cached endpoint URL on first resolution in a session.
status: draft
owner: SAIF Documentation
last_reviewed: 2026-09-17
library: SAIF.Framework.Web.Mvc
reference_type: package
operation_id: OP-15
trigger: Autofac `InstancePerRequest` resolution of `IWebContextService`
---

# Initialize the web context service for a session

Set up the session-backed context dictionary and cached endpoint URL on first resolution in a session. Trigger: Autofac `InstancePerRequest` resolution of `IWebContextService`.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-15 |
| **Trigger** | Autofac `InstancePerRequest` resolution of `IWebContextService` |
| **Inputs** | `IEnvironmentService` |
| **Outputs** | Session-backed context dictionary and cached service URL |
| **Primary components** | `WebContextService` constructor |
| **Entry point** | `src/Web/WebContextService.cs` |

## Steps

1. Read `HttpContext.Current.Session`.
2. When `Session["WebContextRepository"]` is null, create a new `Dictionary<string, IWebContext>` there ([BR-32](../business-rules.md#br-32-web-context-repository-created-once-per-session)).
3. When `Session["ContextServiceUrl"]` is null, populate it from `environmentService.GetConfigSetting(EnvironmentConfigKeys.WebContextServiceUrl)` — the endpoint is resolved once per session, not per request ([BR-33](../business-rules.md#br-33-context-service-endpoint-resolved-once-per-session)).
4. Assign the session dictionary to `items` and the URL to `serviceUrl`.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([IWebContextService resolved])
	Session[Read HttpContext.Current.Session]
	Repo{WebContextRepository present?}
	InitRepo[Create session dictionary]
	Url{ContextServiceUrl present?}
	InitUrl[Resolve webContextServiceUrl setting]
	EndOk([Constructor complete])
	Start --> Session
	Session --> Repo
	Repo -->|No| InitRepo
	Repo -->|Yes| Url
	InitRepo --> Url
	Url -->|No| InitUrl
	Url -->|Yes| EndOk
	InitUrl --> EndOk
```

## Failure and edge-case behavior

- No unexpected failure paths beyond argument-guard exceptions and delegated dependency behavior were identified for this operation.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

## Sources
- `src/Web/WebContextService.cs`
