# TraconServiceCollectionExtensions

> Tracon documentation. Tracon is a .NET package family that adds a control
> plane on Microsoft Agent Framework, published as NuGet packages that run
> inside your own ASP.NET Core application.
>
> Page: https://tracon.dev/api/tracon.traconservicecollectionextensions/
> Summary: TraconServiceCollectionExtensions: Extensions that register Tracon with dependency injection.

<div class="api-provenance">Namespace Tracon · Assembly <code>Tracon.Core.dll</code></div>

Extensions that register Tracon with dependency injection.

```csharp
public static class TraconServiceCollectionExtensions
```

<dl class="api-relations"><dt>Inheritance</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a> ← <a href="/api/tracon.traconservicecollectionextensions/">TraconServiceCollectionExtensions</a></dd><dt>Inherited Members</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a></dd></dl>

## Methods

### <a id="Tracon_TraconServiceCollectionExtensions_AddJobHandler__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String_"></a> AddJobHandler<THandler\>\(IServiceCollection, string\)

Adds an [IJobHandler](/api/tracon.ijobhandler/) extension point under a handler key.

```csharp
public static IServiceCollection AddJobHandler<THandler>(this IServiceCollection services, string handlerKey) where THandler : class, IJobHandler
```

#### Parameters

`services` [IServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)

Service collection.

`handlerKey` [string](https://learn.microsoft.com/dotnet/api/system.string)

The key jobs are dispatched by. 1-128 characters: lowercase ASCII
letters, digits, `.`, `_`, or `-`, starting with a
letter or digit. Pick a namespace prefix of your own
(`contoso.nightly-report`).

#### Returns

 [IServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)

The same collection, for chaining.

#### Type Parameters

`THandler` 

The handler type to add.

#### Remarks

The worker dispatches by exact, ordinal key match, so
**this call may appear before or after `AddTracon`**
— registration order does not decide which handler runs, and no handler
can shadow another.

The handler is registered **scoped** and resolved from a
fresh scope for every execution, so it may take scoped dependencies in
its constructor. Registering two handlers under the same key, or using
a key inside Tracon's own `tracon.` namespace, stops the
host from starting.

```csharp
builder.Services.AddJobHandler<NightlyReportJobHandler>("contoso.nightly-report");
```

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

`services` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

 [ArgumentException](https://learn.microsoft.com/dotnet/api/system.argumentexception)

`handlerKey` is empty, malformed, or inside the
[JobHandlerKeys.ReservedPrefix](/api/tracon.jobhandlerkeys/#Tracon_JobHandlerKeys_ReservedPrefix) namespace.

### <a id="Tracon_TraconServiceCollectionExtensions_AddTracon_Microsoft_Extensions_Hosting_IHostApplicationBuilder_"></a> AddTracon\(IHostApplicationBuilder\)

Adds Tracon to the host application builder and reads settings from
the `Tracon` section.

```csharp
public static ITraconBuilder AddTracon(this IHostApplicationBuilder builder)
```

#### Parameters

`builder` [IHostApplicationBuilder](https://learn.microsoft.com/dotnet/api/microsoft.extensions.hosting.ihostapplicationbuilder)

The host application builder.

#### Returns

 [ITraconBuilder](/api/tracon.itraconbuilder/)

The configuration chain.

#### Remarks

The one call every Tracon application starts with. On its own it
runs with in-memory stores and needs no database; a provider and a store
are added to the chain it returns.

```csharp
var builder = WebApplication.CreateBuilder(args);

builder.AddTracon()
       .UseOpenAI(builder.Configuration["OpenAI:ApiKey"]!)
       .UsePostgreSql(builder.Configuration.GetConnectionString("Tracon")!);
```

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

`builder` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

### <a id="Tracon_TraconServiceCollectionExtensions_AddTracon_Microsoft_Extensions_DependencyInjection_IServiceCollection_Microsoft_Extensions_Configuration_IConfiguration_"></a> AddTracon\(IServiceCollection, IConfiguration?\)

Adds Tracon to the service collection.

```csharp
public static ITraconBuilder AddTracon(this IServiceCollection services, IConfiguration? configurationSection = null)
```

#### Parameters

`services` [IServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)

Service collection.

`configurationSection` [IConfiguration](https://learn.microsoft.com/dotnet/api/microsoft.extensions.configuration.iconfiguration)?

Configuration section settings are read from.

#### Returns

 [ITraconBuilder](/api/tracon.itraconbuilder/)

The configuration chain.

#### Remarks

All services are registered with `TryAdd`. If you register your own
implementation *before* this call, yours wins; Tracon does not overwrite it.

When no additional configuration is done, Tracon runs with
in-memory stores and requires no database.

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

`services` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

### <a id="Tracon_TraconServiceCollectionExtensions_UseScheduling_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Tracon_TraconSchedulingOptions__"></a> UseScheduling\(IServiceCollection, Action<TraconSchedulingOptions\>?\)

Sets batch and scheduled run settings from code.

```csharp
public static IServiceCollection UseScheduling(this IServiceCollection services, Action<TraconSchedulingOptions>? configure = null)
```

#### Parameters

`services` [IServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)

Service collection.

`configure` [Action](https://learn.microsoft.com/dotnet/api/system.action\-1)<[TraconSchedulingOptions](/api/tracon.traconschedulingoptions/)\>?

Settings modifier. When not given, only the defaults/configuration apply.

#### Returns

 [IServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)

The same collection, for chaining.

#### Remarks

The job queue and scheduling stores are already registered by
`AddTracon`; this method only changes the settings (e.g.
turning off this process's background worker with
`o.RunWorker = false`). `PostConfigure` is used, so the value
given from code always wins over the value coming from the configuration file.

```csharp
// A web instance that serves requests and leaves the jobs to a worker process.
builder.Services.UseScheduling(o => o.RunWorker = false);
```

#### Exceptions

 [ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

`services` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).
