> ## Documentation Index
> Fetch the complete documentation index at: https://docs.westyx.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# .NET SDK - Dependency injection

> Register the Nexus client with AddWestyxNexus() - IHttpClientFactory, ILoggerFactory, and asynchronous startup.

*Package: `WestyxNexus.Extensions.DependencyInjection` (v0.11.0+)*

`AddWestyxNexus()` registers the client as a singleton, wires it through `IHttpClientFactory` and the application's `ILoggerFactory`, and performs the initial sync during host startup.

```sh theme={null}
dotnet add package WestyxNexus.Extensions.DependencyInjection
```

```csharp theme={null}
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddWestyxNexus(options =>
{
    options.BaseUrl = builder.Configuration["Nexus:BaseUrl"]!;
    options.ApiKey  = builder.Configuration["Nexus:ApiKey"];
});

var app = builder.Build();
```

Then inject `NexusClient` anywhere:

```csharp theme={null}
public sealed class CheckoutService(NexusClient nexus)
{
    public bool NewFlowEnabled() => nexus.GetFlag("new.checkout");
}
```

## Options

| Property               | Default               | Notes                                                                 |
| ---------------------- | --------------------- | --------------------------------------------------------------------- |
| `BaseUrl`              | *(required)*          | The full service endpoint, e.g. `https://blue-ocean-a5rx7.westyx.dev` |
| `ApiKey`               | -                     | Required unless `Wif` is enabled                                      |
| `Ttl`                  | 60 s                  | Cache lifetime before a background refresh                            |
| `Wif`                  | -                     | Workload Identity Federation config                                   |
| `Observer`             | -                     | `IStreamObserver` for SSE lifecycle telemetry                         |
| `SseReconnectCooldown` | `[5, 10, 20, 40, 60]` | Reconnect schedule, in minutes                                        |
| `EnableStream`         | `true`                | Start the SSE live-update stream after initialisation                 |

`HttpClient` and `StreamHandler` are deliberately absent. On this path the HTTP clients come from `IHttpClientFactory`, which is the point of using it - configure them by name instead.

## Startup is asynchronous

The initial sync is a network call. A plain singleton factory would have to block:

```csharp theme={null}
// Do not do this. It blocks a thread-pool thread during host startup.
services.AddSingleton(sp => NexusClient.CreateAsync(cfg).GetAwaiter().GetResult());
```

`AddWestyxNexus()` instead starts the work once and awaits it in an `IHostedService`, so host startup stays properly asynchronous and a misconfiguration surfaces as a **startup** failure rather than as a failure on the first request.

The hosted service is inserted at the front of the service collection, so the client is ready before any hosted service you registered can resolve it.

## Customising the HTTP clients

Two named clients are registered:

| Name                                                    | Used for                                      | Timeout  |
| ------------------------------------------------------- | --------------------------------------------- | -------- |
| `NexusHttpClientNames.Sync` (`"westyx-nexus"`)          | `/v1/sync`, WIF token exchange, AB evaluation | 10 s     |
| `NexusHttpClientNames.Stream` (`"westyx-nexus-stream"`) | The long-lived SSE connection                 | infinite |

Add your own handlers, retry policies or timeouts to either:

```csharp theme={null}
builder.Services.AddHttpClient(NexusHttpClientNames.Sync)
    .AddPolicyHandler(myRetryPolicy);
```

### Handler rotation is safe for SSE

`IHttpClientFactory` rotates handlers every two minutes, which looks fatal for a connection that stays open for hours. It is not: the factory only stops handing an expired handler to **new** requests - a request already in flight keeps its handler until it completes.

<Warning>
  Do not "fix" this with `SetHandlerLifetime(Timeout.InfiniteTimeSpan)`. That reintroduces exactly the stale-DNS problem the factory exists to solve: a process that never recycles a connection never observes an ingress reconfiguration or a load-balancer replacement.
</Warning>

## Next

* [Secrets binding](/sdks/dotnet/secrets-binding) - `IOptions<T>` for secrets
* [Feature management](/sdks/dotnet/feature-management) - Nexus flags through `IFeatureManager`
* [ASP.NET Core configuration](/sdks/dotnet/aspnetcore-configuration) - configs through `IConfiguration`
* [Design decisions](/sdks/dotnet/design-decisions)
