Inject Harness Dependencies
Tool harness dependencies use ordinary constructor parameters. The source generator emits direct activation code that resolves those parameters without reflection.
One Public Constructor
public sealed class SearchHarness(
IConfig<SearchConfig> configuration,
ISearchClient client)
{
[AIFunction]
public Task<string> Search(string query)
=> client.SearchAsync(query, configuration.AgentDefaults.MaxResults);
}A harness has exactly one public constructor. It may mix:
- one
IConfig<T>settings parameter; - ordinary service parameters;
- optional parameters with valid compile-time defaults.
Generated activation does not infer special parameterless, config-only, or ISecretResolver-only constructor shapes. The single public constructor is the activation contract.
Resolve A Dependency
Dependencies resolve in this order:
Registration-local UseDependency
→ application DI
→ library-declared default
→ missing-dependency errorOverride one service for this harness registration:
assistant.WithToolHarness<SearchHarness>(options => options
.UseDependency<ISearchClient>(customClient));The supplied instance is borrowed. The application remains responsible for its lifetime.
UseDependency(...) affects constructor activation. It does not populate typed metadata for capabilities such as [AIFunction<TMetadata>]. Supply metadata through the registration context argument when the harness needs both:
assistant.WithToolHarness<SearchHarness>(
options => options.UseDependency<ISearchClient>(customClient),
metadata);Use Application DI
services.AddSingleton<ISearchClient, CustomSearchClient>();
var assistant = new AgentConfig()
.WithOpenAI(model: "gpt-5-mini")
.WithToolHarness<SearchHarness>();
await using var runtime = await new AgentRuntimeBuilder()
.WithServiceProvider(serviceProvider)
.AddAgent("assistant", assistant)
.BuildAsync();The service provider belongs to the runtime, so every agent it builds resolves harness dependencies from it. Objects created by DI belong to the DI container. HPD does not dispose them.
Declare A Library Default
[ToolHarnessDependency(typeof(ISearchClient), typeof(SearchClient))]
public sealed class SearchHarness(
IConfig<SearchConfig> configuration,
ISearchClient client)
{
}SearchClient also follows the one-public-constructor rule. HPD owns and disposes defaults it creates.
Two declarations for the same service contract are a configuration conflict. A derived harness does not currently replace a base default implicitly.
Explicit Harness Instances
var harness = new SearchHarness(configurationHandle, client);
assistant.WithToolHarness(harness);An explicit instance uses its current dependencies and captured state. Registration-time .Configure(...) and .UseDependency(...) are rejected for that instance. The caller owns it.
Explicit-instance registration validates immediately:
- A null instance throws
ArgumentNullException. - Registering the same harness instance slot twice throws
InvalidOperationException. - A harness without a generated registration fails when it is registered.
The instance, supplied dependencies, and middleware overrides are process-local parts of the harness reference on the definition. Every agent the runtime builds from that definition uses them, and the definition cannot be written to the agent store. runtime.Agents lists them as harnessInstance, harnessDependency, and harnessMiddlewareOverride parts.
Lifetime Rules
| Object | Owner |
|---|---|
| Generated harness | The agent the runtime built |
Dependency created from [ToolHarnessDependency] | The agent the runtime built |
.UseDependency(...) instance | Caller |
| DI-created service | DI container |
| Explicit harness instance | Caller |
An agent-owned harness constructor must not capture a dependency that requires an execution scope. Use execution-scoped middleware or the execution resource contracts when behavior belongs to one accepted run.