Skip to main content
Table of Contents

Class TrustedTypeResolverServiceCollectionExtensions

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
Stratara.Abstractions.dll

DI extensions that register the ITrustedTypeResolver singleton and populate it with command / event / aggregate types discovered by assembly scanning.

public static class TrustedTypeResolverServiceCollectionExtensions
Inheritance
TrustedTypeResolverServiceCollectionExtensions
Inherited Members

Remarks

The resolver is the framework-wide allowlist consulted whenever a persisted type-name string is converted back to a runtime Type. Code paths that previously called GetType(string) on attacker-controlled input (RabbitMQ / Service Bus envelopes, event-stream rows, snapshot rows) now resolve through the allowlist instead.

Methods

AddAggregatesFromAssemblyContaining<T>(IServiceCollection)

Scans the assembly containing T for concrete IAggregate implementations and registers each in the trusted-type resolver — together with every event type consumed by the aggregate's Apply(...) methods — so both SnapshotService can materialize the aggregate and IAggregationService can replay its event stream without further AddTrustedType<TEvent>() calls.

public static IServiceCollection AddAggregatesFromAssemblyContaining<T>(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection to mutate.

Returns

IServiceCollection

The same services instance for chaining.

Type Parameters

T

A marker type from the assembly to scan.

Examples

Registers each aggregate and every event its Apply methods take, so persisted payloads deserialize:

services.AddAggregatesFromAssemblyContaining<Account>();

Remarks

For each discovered aggregate, every public instance method named Apply that takes a single parameter contributes its parameter type to the allowlist. This mirrors the event-sourcing convention used throughout the framework (see IAggregate).

AddDomainEventTypesFromAssemblyContaining<T>(IServiceCollection)

Scans the assembly containing T and registers only the event types consumed by the aggregates' Apply(TEvent) methods — without registering the aggregate types themselves and without registering any projection / saga / command-handler classes into DI.

public static IServiceCollection AddDomainEventTypesFromAssemblyContaining<T>(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection to mutate.

Returns

IServiceCollection

The same services instance for chaining.

Type Parameters

T

A marker type from the assembly that contains the aggregates and their events.

Examples

For an event-only host — a projection or saga worker that must deserialize payloads without wiring the aggregates' handler dependencies:

services.AddDomainEventTypesFromAssemblyContaining<Account>();

Remarks

Use this in a host that only needs to deserialize event payloads coming off the message bus or event stream — typically a dedicated projection or saga worker — but must not pull the aggregate or its handler classes into the container, because those handlers depend on runtime services the worker does not wire. AddProjectionsFromAssemblyContaining<T> would register both the event types and the handler classes; this method registers the event types alone.

Discovery follows the same convention as AddAggregatesFromAssemblyContaining<T>(IServiceCollection): every public, single-parameter instance method named Apply on a concrete IAggregate contributes its parameter type to the allowlist.

AddTrustedTypeResolver(IServiceCollection)

Registers the ITrustedTypeResolver as a singleton if no implementation has been registered yet.

public static IServiceCollection AddTrustedTypeResolver(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection to mutate.

Returns

IServiceCollection

The same services instance for chaining.

Examples

Idempotent. The assembly-scanning registrations call it, so a host that uses them does not:

services.AddTrustedTypeResolver();

AddTrustedType<T>(IServiceCollection)

Registers T in the trusted-type allowlist. Use for types produced but never directly handled (e.g. aggregate snapshot types whose aggregates have no projection / saga to anchor an automatic scan).

public static IServiceCollection AddTrustedType<T>(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection to mutate.

Returns

IServiceCollection

The same services instance for chaining.

Type Parameters

T

The type to add to the allowlist.

Examples

For a type that is produced but never handled — a snapshot type whose aggregate has no projection or saga to anchor the automatic scan:

services.AddTrustedType<AccountSnapshot>();

GetOrAddResolver(IServiceCollection)

Internal helper used by other scan extensions (AddCommandHandlersFromAssemblyContaining, AddProjectionsFromAssemblyContaining, AddSagasFromAssemblyContaining) to retrieve the shared TrustedTypeResolver singleton during ConfigureServices.

public static TrustedTypeResolver GetOrAddResolver(IServiceCollection services)

Parameters

services IServiceCollection

The service collection.

Returns

TrustedTypeResolver

The shared resolver instance, created and registered on first call.