# TraconContentGuardBuilderExtensions

> 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.traconcontentguardbuilderextensions/
> Summary: TraconContentGuardBuilderExtensions: Chain extensions that turn on content inspection.

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

Chain extensions that turn on content inspection.

```csharp
public static class TraconContentGuardBuilderExtensions
```

<dl class="api-relations"><dt>Inheritance</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a> ← <a href="/api/tracon.traconcontentguardbuilderextensions/">TraconContentGuardBuilderExtensions</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>

## Remarks

This is an extension method, not a member of [ITraconBuilder](/api/tracon.itraconbuilder/):
adding a member to the interface is a breaking change after release; adding
an extension method is not.

## Methods

### <a id="Tracon_TraconContentGuardBuilderExtensions_AddContentGuard__1_Tracon_ITraconBuilder_"></a> AddContentGuard<TGuard\>\(ITraconBuilder\)

Registers your own [IContentGuard](/api/tracon.icontentguard/) implementation.

```csharp
public static ITraconBuilder AddContentGuard<TGuard>(this ITraconBuilder builder) where TGuard : class, IContentGuard
```

#### Parameters

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

#### Returns

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

#### Type Parameters

`TGuard` 

The guard type.

#### Remarks

Multiple guards can be registered; all of them run in sequence and
**the strictest decision wins**.

```csharp
builder.AddTracon()
       .AddPatternContentGuard()
       .AddContentGuard<CustomerNameGuard>();
```

#### 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_TraconContentGuardBuilderExtensions_AddContentGuard_Tracon_ITraconBuilder_Tracon_IContentGuard_"></a> AddContentGuard\(ITraconBuilder, IContentGuard\)

Registers a configured [IContentGuard](/api/tracon.icontentguard/) instance.

```csharp
public static ITraconBuilder AddContentGuard(this ITraconBuilder builder, IContentGuard guard)
```

#### Parameters

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

`guard` [IContentGuard](/api/tracon.icontentguard/)

The guard instance.

#### Returns

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

#### Remarks

Multiple guards can be registered; all of them run in sequence and
the strictest decision wins. The caller owns the instance and any
resources it holds.

#### Exceptions

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

### <a id="Tracon_TraconContentGuardBuilderExtensions_AddContentGuard_Tracon_ITraconBuilder_System_Func_System_IServiceProvider_Tracon_IContentGuard__"></a> AddContentGuard\(ITraconBuilder, Func<IServiceProvider, IContentGuard\>\)

Registers an [IContentGuard](/api/tracon.icontentguard/) factory.

```csharp
public static ITraconBuilder AddContentGuard(this ITraconBuilder builder, Func<IServiceProvider, IContentGuard> factory)
```

#### Parameters

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

`factory` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<[IServiceProvider](https://learn.microsoft.com/dotnet/api/system.iserviceprovider), [IContentGuard](/api/tracon.icontentguard/)\>

The factory that creates the guard.

#### Returns

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

#### Remarks

The container owns the produced singleton. The factory must not capture
a scoped dependency because the result outlives that scope.

#### Exceptions

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

### <a id="Tracon_TraconContentGuardBuilderExtensions_AddPatternContentGuard_Tracon_ITraconBuilder_System_Action_Tracon_PatternContentGuardOptions__"></a> AddPatternContentGuard\(ITraconBuilder, Action<PatternContentGuardOptions\>?\)

Registers Tracon's built-in pattern-based content guard.

```csharp
public static ITraconBuilder AddPatternContentGuard(this ITraconBuilder builder, Action<PatternContentGuardOptions>? configure = null)
```

#### Parameters

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

`configure` [Action](https://learn.microsoft.com/dotnet/api/system.action\-1)<[PatternContentGuardOptions](/api/tracon.patterncontentguardoptions/)\>?

Pattern and denied-term settings.

#### Returns

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

#### Remarks

**This call is the no-surprises rule's gate.** `AddTracon` alone
registers no guard, and the inspection wrapper is *never added* to
the model pipeline. Without this call no prompt is inspected, no response
is inspected, and no cost is paid.

The built-in guard carries **no rules by default**:
[PatternContentGuardOptions.MaskedPii](/api/tracon.patterncontentguardoptions/#Tracon_PatternContentGuardOptions_MaskedPii) is
[PiiPatterns.None](/api/tracon.piipatterns/) and
[PatternContentGuardOptions.DeniedTerms](/api/tracon.patterncontentguardoptions/#Tracon_PatternContentGuardOptions_DeniedTerms) is empty. Which
pattern family to turn on is an explicit choice; turning them all on at
once compounds the false-positive risk.

The same settings are also read from the `Tracon:ContentGuard:Pattern`
configuration section; if that section is present the guard is already
registered by `AddTracon` and this call is redundant (the
registration uses `TryAddEnumerable`, so it is never added twice).

```csharp
builder.AddTracon()
       .AddPatternContentGuard(options =>
       {
           options.MaskedPii = PiiPatterns.CreditCard | PiiPatterns.Email;
           options.DeniedTerms.Add("secret-project");
       });
```

#### Exceptions

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