Skip to content

ServiceRegistrationAttribute

ServiceRegistrationAttribute is a declarative way to mark a class for DI registration without writing an explicit fluent selector. The Specular generator reads the attribute at build time and includes the type in the generated provider; no runtime reflection occurs.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ServiceRegistrationAttribute : Attribute

Applied to a class, it instructs the generator to register the class in the DI container. The lifetime is always required; if no explicit service types are given, the type is registered against every interface it implements (plus itself).

Constructors:

// Register against explicit service types with a required lifetime
ServiceRegistrationAttribute(ServiceLifetime lifetime, params Type[] serviceTypes)

Properties:

PropertyTypeDescription
ServiceTypesImmutableArray<Type>The service types to register against. Empty means register against all implemented interfaces (plus self).
LifetimeServiceLifetimeThe DI lifetime. Always required.

AllowMultiple = true means a single class can carry multiple [ServiceRegistration] attributes to be registered against different service types or with different lifetimes simultaneously. The generic and non-generic forms can be mixed freely on the same class.

When a class carries more than one attribute:

  • The class itself is registered once, using the lifetime of the first attribute (in declaration order).
  • Each service type is registered with the lifetime of the attribute that names it, and resolves to that single registration of the class.
  • A service type named by more than one attribute (whether generic or not) reports RSGD0005 — a class cannot register the same service type twice.

The generic variants provide a concise way to specify one to eight explicit service types without passing typeof(...) arguments.

[ServiceRegistration<IMyService>(ServiceLifetime.Scoped)]
public class MyService : IMyService { }

Registers MyService as IMyService with the given lifetime.

ServiceRegistrationAttribute<TService1, TService2>

Section titled “ServiceRegistrationAttribute<TService1, TService2>”
[ServiceRegistration<IMyService, IAlternate>(ServiceLifetime.Scoped)]
public class MyService : IMyService, IAlternate { }

Registers against two service types.

ServiceRegistrationAttribute<TService1, TService2, TService3> through <..., TService8>

Section titled “ServiceRegistrationAttribute<TService1, TService2, TService3> through <..., TService8>”

Registers against up to eight explicit service types. Every generic variant requires a ServiceLifetime constructor argument — there is no default.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class RegistrationLifetimeAttribute : Attribute

Overrides the DI lifetime for a type independently of [ServiceRegistration]. Useful when you want to set a single lifetime on a base class or interface and have it apply across all registrations of that type.

public RegistrationLifetimeAttribute(ServiceLifetime lifetime)
PropertyTypeDescription
LifetimeServiceLifetimeThe overriding lifetime.
public static IServiceCollection AddSpecularServiceRegistrations(
this IServiceCollection services,
ISpecularProvider provider
)

Extension method on IServiceCollection that uses provider.Scan() to find all classes decorated with any [ServiceRegistration] variant and adds them to the container. The scan itself is resolved at build time; this call at startup simply replays the pre-built registrations.

Usage:

// In Program.cs / Startup.cs
services.AddSpecularServiceRegistrations(SpecularProvider.Instance);
// Scoped service registered as IMyService — lifetime from [ServiceRegistration] itself
[ServiceRegistration<IMyService>(ServiceLifetime.Scoped)]
public class MyService : IMyService { }
// Class carrying a separate lifetime override via [RegistrationLifetime]
[ServiceRegistration(ServiceLifetime.Singleton)]
[RegistrationLifetime(ServiceLifetime.Scoped)]
public class AnotherService : IAnotherService { }
// Multiple registrations on one class
[ServiceRegistration<IMyService>(ServiceLifetime.Singleton)]
[ServiceRegistration<IAlternate>(ServiceLifetime.Transient)]
public class DualService : IMyService, IAlternate { }
// Wire up everything in one call
builder.Services.AddSpecularServiceRegistrations(SpecularProvider.Instance);
  • Registration is a compile-time operation. The generator discovers [ServiceRegistration]-decorated types during the build, not at application startup.
  • Abstract classes and static classes are implicitly excluded — AddClasses() skips them by default.
  • If ServiceTypes is empty (i.e. the attribute is applied with no explicit type arguments), the class is registered against itself and every interface it implements.
  • Open generic classes are registered by their type definitions, so the non-generic form is the one to use for them: [ServiceRegistration(ServiceLifetime.Scoped, typeof(IRepository<>))] on Repository<T>. The generic attributes cannot name an open generic service type (attribute type arguments cannot use type parameters).