Autofac
AppSurfaceAutofacExtensions
Source
Provides extension methods for Autofac's ContainerBuilder
to simplify common registrations.
Remarks
These helpers are convenience wrappers for assembly-scoped reflection scanning. Prefer explicit Autofac registration when ordering matters, multiple implementations need different lifetimes, startup performance is sensitive, or AOT/linker trimming must preserve only known types.
RegisterImplementations
2 overloads
IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> RegisterImplementations<TInterface>(this ContainerBuilder builder)
Registers all non-abstract class implementations of the specified interface type found in the interface's assembly.
Type parameters
-
TInterfaceThe interface type to scan for implementations of.
Parameters
-
builderThe container builder.
Returns
A registration builder for the scanned types.
Remarks
This method scans only the assembly that declares TInterface
and registers concrete, non-abstract assignable classes as TInterface
services. It returns Autofac's IRegistrationBuilder{TLimit,TActivatorData,TRegistrationStyle}
so callers can add lifetime, ownership, and metadata configuration. Use this helper when one interface owns a small assembly-local plugin surface and interface resolution is the intended contract. Prefer explicit registrations when implementations cross assemblies, require different service interfaces, need distinct lifetimes, or must be linker/AOT-friendly. Reflection scanning recovers from partial type-load failures by registering successfully loaded types only, so missing optional dependencies can still hide implementations that failed to load.
IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> RegisterImplementations<TInterface>(this ContainerBuilder builder, Func<Assembly, Type[]> getTypes)
Registers implementations of TInterface
using a caller-provided assembly type loader.
Type parameters
-
TInterfaceThe interface type to scan for implementations of.
Parameters
-
builderThe container builder. -
getTypesType loader for the assembly that declaresTInterface.
Returns
A registration builder for the scanned types.
Exceptions
-
InvalidOperationExceptionThrown whengetTypescompletes successfully but returnsnull.
Remarks
This internal seam exists so tests can verify partial-load recovery without creating a broken fixture assembly. The getTypes
delegate must return a non-null array for successful loads. If it throws ReflectionTypeLoadException
, successfully loaded non-null entries from ReflectionTypeLoadException.Types
are still registered and failed entries are ignored.
GetLoadableTypes
IEnumerable<Type> GetLoadableTypes(Assembly assembly, Func<Assembly, Type[]> getTypes)
Gets all loadable types from an assembly while tolerating partial reflection-load failures.
Parameters
-
assemblyThe assembly being scanned. -
getTypesThe delegate used to load the assembly's type array.
Returns
All loaded types, or the non-null subset from ReflectionTypeLoadException.Types
.
Exceptions
-
InvalidOperationExceptionThrown whengetTypescompletes successfully but returnsnull.
AppSurfaceAutofacHostModule
SourceA base module that integrates Autofac into the AppSurface host lifecycle.
AppSurfaceAutofacModule
Source
An Autofac Module
wrapper that also participates in AppSurface module discovery.
Remarks
AppSurfaceAutofacModule
implements IAppSurfaceModule
so Autofac-backed modules can be discovered with the rest of the AppSurface graph. Put Autofac registrations in normal Autofac module methods such as Module.Load(ContainerBuilder)
; ConfigureServices
is intentionally a no-op for interface compatibility. Override RegisterDependentModules
when this module requires other AppSurface modules. Pitfall: Autofac registration ordering and module dependencies still matter, so do not expect ConfigureServices
to bridge Microsoft DI registrations into Autofac.
ConfigureServices
void ConfigureServices(StartupContext context, IServiceCollection services)
Intentionally does not register Microsoft DI services for this Autofac module.
Parameters
-
contextThe current startup context. -
servicesThe Microsoft DI service collection, unused by this module.
Remarks
Use Autofac's Module.Load(ContainerBuilder)
for registrations. This method exists only to satisfy IAppSurfaceModule
.
RegisterDependentModules
void RegisterDependentModules(ModuleDependencyBuilder builder)
Registers AppSurface module dependencies required before this Autofac module is used.
Parameters
-
builderThe module dependency builder used to declare prerequisites.