> For the complete documentation index, see [llms.txt](https://docs.pal.aic.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pal.aic.io/foundations/dependency-injection.md).

# Dependency injection

PAL.NET uses Microsoft.Extensions.DependencyInjection. Registration is compositional: register the base runtime once, then add only the capability packages needed by the application.

## Core registration

```csharp
services.AddPalantir(options);
```

This registers:

* validated singleton `PalantirClientOptions`;
* retry policy, response reader, diagnostics, and system time provider;
* authenticated HTTP transport and request factory;
* scoped `IPalantirTransport`, `IPalantirClient`, and `IPalPlatformOperationClient`;
* fail-closed default token provider and licence guard.

## Capability registration

```csharp
services.AddPalantirGeneratedPlatform();
services.AddPalantirOntology();
services.AddPalantirDatasets();
services.AddPalantirSql();
services.AddPalantirAipAgents();
```

`AddPalantirGeneratedPlatform` registers the generated capability clients as scoped services. The release currently contains 21 generated capability families and 321 public generated operations in the pinned baseline. The higher-level registrations add their own scoped façade clients.

## Foundation composition

Foundation hosts call:

```csharp
services.RegisterPlatformCoreServices(
    configuration,
    includeVaultServices: false,
    includeBackgroundServices: true,
    includeLogging: true);
```

PAL.NET is registered only when the `Palantir` section exists. Foundation's sequence is configuration, logging, core infrastructure, identity, repositories, serialization, optional PAL.NET, then web/background services. PAL registration validates local configuration and performs no remote work.

## Replacing defaults

Use normal DI replacement after the base registration:

```csharp
services.AddPalantir(options);
services.AddScoped<IPalantirAccessTokenProvider, MyAccessTokenProvider>();
services.AddSingleton<IPalantirLicenseGuard, MyLicenseGuard>();
```

For tests, replace the transport, token provider, licence guard, clock, and jitter source with deterministic implementations. Keep capability client lifetimes scoped to the operation context.

## Lifetime guidance

* Keep the service provider long-lived for the host lifetime.
* Create a scope per request, message, function invocation, or background job.
* Do not put scoped PAL clients into singletons.
* Dispose an `IServiceScope` after streamed results and all child operations have completed.
* Do not cache access tokens in application code unless the security boundary and scope rules are explicit; use the approved token provider.

## No hidden network work

Calling any registration method should be safe during startup validation. Token retrieval, licence file reads, and Palantir requests begin only when an operation is invoked.
