A .NET library that auto-registers services via marker interfaces and attributes — supports all lifetimes and open generics.
🇹🇷 Türkçe için README.tr.md
dotnet add package AssemblyServiceRegistrar// Mark your interfaces with a lifetime marker
public interface IUserService : IScopedService { ... }
public class UserService : IUserService { ... }
// Register everything in one call
builder.Services.AddServicesFromAssembly(Assembly.GetExecutingAssembly());That's it — no manual registration needed.
- Automatic Assembly Scanning — scans one or more assemblies and registers services automatically
- Marker Interface Pattern — use
ISingletonService,IScopedService, orITransientServiceto declare lifetime - Attribute Support —
[ServiceRegistration]as an alternative, with self-registration - Open Generic Support — registers open generic services such as
IRepository<T> - Filtering — include/exclude types via a
Func<Type, bool>predicate - Safe Registration — optional
tryAddto avoid duplicate registrations - Resilient Scanning — tolerates
ReflectionTypeLoadException(skips unloadable types)
- .NET 8.0 or later
Microsoft.Extensions.DependencyInjection
dotnet add package AssemblyServiceRegistrar# Package Manager Console
Install-Package AssemblyServiceRegistrar<PackageReference Include="AssemblyServiceRegistrar" Version="1.0.0" />git clone https://github.com/sametbrr/AssemblyServiceRegistrar.git
cd AssemblyServiceRegistrar
dotnet buildusing AssemblyServiceRegistrar;
public interface IConfigurationService : ISingletonService
{
string GetConnectionString();
}
public interface IUserService : IScopedService
{
Task<User> GetUserAsync(int id);
Task CreateUserAsync(User user);
}
public interface IEmailService : ITransientService
{
Task SendEmailAsync(string to, string subject, string body);
}builder.Services.AddServicesFromAssembly(Assembly.GetExecutingAssembly());builder.Services.AddServicesFromAssemblies(
Assembly.GetExecutingAssembly(),
typeof(SomeTypeInAnotherAssembly).Assembly);// Self-registration
[ServiceRegistration(ServiceLifetime.Singleton)]
public class CacheManager { }
// Explicit service type
[ServiceRegistration(ServiceLifetime.Scoped, typeof(IReportService))]
public class ReportService : IReportService { }public interface IRepository<T> : IScopedService { }
public class Repository<T> : IRepository<T> { }
// IRepository<> → Repository<> registered automatically
builder.Services.AddServicesFromAssembly(Assembly.GetExecutingAssembly());// Exclude specific namespaces
builder.Services.AddServicesFromAssembly(
Assembly.GetExecutingAssembly(),
filter: t => !t.Namespace!.Contains("Internal"));
// Avoid duplicate registrations
builder.Services.AddServicesFromAssembly(
Assembly.GetExecutingAssembly(),
tryAdd: true);- Scans all concrete (non-abstract) classes in the specified assemblies.
- A class is a candidate if it implements an
IService-derived interface or carries[ServiceRegistration]. - Determines lifetime:
[ServiceRegistration(lifetime)]takes precedence if presentISingletonService→ServiceLifetime.SingletonIScopedService→ServiceLifetime.ScopedITransientService→ServiceLifetime.Transient- A class implementing more than one lifetime marker throws
InvalidOperationException
- Registers against each
IService-derived interface (marker interfaces excluded); open generics are registered as open generic definitions.
namespace AssemblyServiceRegistrar
{
public interface IService { }
public interface IScopedService : IService { }
public interface ISingletonService : IService { }
public interface ITransientService : IService { }
}- Use
ISingletonServicefor stateless services and configurations - Use
IScopedServicefor services shared within a request scope - Use
ITransientServicefor lightweight, stateless services - Keep interfaces focused and follow the Single Responsibility Principle
- Interface-based registration only covers interfaces deriving from
IService— use[ServiceRegistration]for others or self-registration - A class must not implement more than one lifetime marker interface
- Constructor/parameter-based conditional registration is not supported
MIT — see LICENSE.txt.