A powerful, convention-based automatic service discovery and registration library for .NET 9.0 applications. This library transforms manual dependency injection configuration into an intelligent, attribute-driven system that can discover, validate, and register services automatically while maintaining full control and flexibility.
Transform your service registration from manual configuration to intelligent automation:
// 1. Mark your services with attributes
[ServiceRegistration(ServiceLifetime.Scoped)]
public class UserService : IUserService
{
public async Task<User> GetUserAsync(int id)
{
return new User { Id = id, Name = "Sample User" };
}
}
// 2. Enable automatic discovery in Program.cs
var builder = WebApplication.CreateBuilder(args);
// Single line to automatically discover and register all services
builder.Services.AddAutoServices();
var app = builder.Build();
That's it! Your IUserService
is now automatically discovered, registered, and ready for dependency injection.
Automatically discover services using intelligent naming conventions and attributes:
[ServiceRegistration(ServiceLifetime.Scoped)]
public class ProductService : IProductService { }
[ServiceRegistration(ServiceLifetime.Singleton)]
public class CacheService : ICacheService { }
Register different implementations based on environment:
[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Development")]
public class MockEmailService : IEmailService { }
[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Production")]
public class SmtpEmailService : IEmailService { }
Use powerful, type-safe conditional logic:
[ConditionalService(ctx =>
ctx.Environment.IsProduction() &&
ctx.FeatureEnabled("AdvancedLogging") &&
!ctx.Configuration.GetValue<bool>("MaintenanceMode"))]
[ServiceRegistration(ServiceLifetime.Scoped)]
public class AdvancedLoggingService : ILoggingService { }
Build complex configurations with readable, chainable syntax:
builder.Services.ConfigureAutoServices()
.FromCurrentDomain(assembly => !assembly.FullName.StartsWith("System"))
.WithProfile(ctx => ctx.Environment.IsDevelopment() ? "Dev" : "Prod")
.When(ctx => ctx.FeatureEnabled("AutoDiscovery"))
.ExcludeNamespaces("MyApp.Internal.*", "MyApp.Testing.*")
.WithPerformanceOptimizations()
.Apply();
Optimized for production with advanced caching and parallel processing:
builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
options.EnableParallelProcessing = true;
options.EnablePerformanceMetrics = true;
options.MaxDegreeOfParallelism = 4;
});
graph TB
A[Service Classes] --> B[Assembly Scanner]
B --> C[Attribute Processor]
C --> D[Naming Convention Resolver]
D --> E[Conditional Evaluator]
E --> F[Plugin Coordinator]
F --> G[Performance Cache]
G --> H[Service Registration]
I[Configuration] --> E
J[Environment Context] --> E
K[Feature Flags] --> E
L[Custom Plugins] --> F
style A fill:#e1f5fe
style H fill:#c8e6c9
style G fill:#fff3e0
style F fill:#f3e5f5
The library follows a sophisticated pipeline architecture where each component has a specific responsibility:
- Assembly Scanner: Efficiently discovers service candidates using reflection
- Attribute Processor: Interprets registration attributes and metadata
- Naming Convention Resolver: Applies intelligent interface-to-implementation mapping
- Conditional Evaluator: Processes environment and configuration-based conditions
- Plugin Coordinator: Manages extensible discovery strategies
- Performance Cache: Optimizes repeated discovery operations
- Service Registration: Final registration with dependency injection container
- Getting Started Guide - Step-by-step introduction
- Service Registration - Attribute-based service marking
- Naming Conventions - Interface resolution strategies
- Conditional Registration - Environment and configuration-based logic
- Fluent Configuration - Advanced configuration patterns
- Plugin Architecture - Extensible discovery mechanisms
- Performance Optimization - Caching and parallel processing
- Expression-Based Conditions - Type-safe conditional logic
- System Architecture - Detailed architectural overview
- Custom Naming Conventions - Building custom resolution logic
- Plugin Development - Creating discovery extensions
- Performance Monitoring - Metrics and optimization
dotnet add package FS.AutoServiceDiscovery.Extensions
Requirements:
- .NET 9.0 or later
- Microsoft.Extensions.DependencyInjection 9.0.0+
[ServiceRegistration(ServiceLifetime.Scoped)]
public class UserService : IUserService
{
// Automatically registered as IUserService -> UserService
}
[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Development")]
public class DevUserService : IUserService { }
[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Production")]
public class ProdUserService : IUserService { }
[ConditionalService(ctx =>
ctx.Environment.IsProduction() &&
ctx.Configuration.GetValue<bool>("Features:EnableCaching") &&
ctx.EvaluateCustomCondition("DatabaseHealthy"))]
[ServiceRegistration(ServiceLifetime.Singleton)]
public class RedisCacheService : ICacheService { }
// Optimized for large applications
builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
options.EnableParallelProcessing = true;
options.EnablePerformanceMetrics = true;
options.EnableLogging = false; // Reduce overhead in production
});
// Get performance statistics
var stats = PerformanceServiceCollectionExtensions.GetCacheStatistics();
Console.WriteLine($"Cache Hit Ratio: {stats.HitRatio:F1}%");
builder.Services.AddAutoServices(options =>
{
options.Profile = builder.Environment.EnvironmentName;
options.Configuration = builder.Configuration;
options.EnableLogging = true;
options.IsTestEnvironment = false;
});
builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
options.EnableParallelProcessing = true;
options.MaxDegreeOfParallelism = Environment.ProcessorCount;
options.EnablePerformanceMetrics = true;
options.EnableLogging = false;
});
builder.Services.ConfigureAutoServices()
.FromAssemblies(Assembly.GetExecutingAssembly())
.WithProfile("Production")
.When(ctx => ctx.FeatureEnabled("AutoDiscovery"))
.ExcludeTypes(type => type.Name.EndsWith("Test"))
.WithDefaultLifetime(ServiceLifetime.Scoped)
.WithPerformanceOptimizations()
.Apply();
Feature | Impact | Best Use Case |
---|---|---|
Basic Discovery | ~10-50ms | Small to medium applications |
Cached Discovery | ~1-5ms | Repeated discovery operations |
Parallel Processing | 2-4x faster | Large applications (100+ services) |
Plugin System | Variable | Complex discovery requirements |
We welcome contributions! This project is open source and benefits from community involvement:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Development Guidelines:
- Follow existing code patterns and conventions
- Add comprehensive tests for new features
- Update documentation for any public API changes
- Ensure backward compatibility when possible
This project is licensed under the MIT License. See the LICENSE file for details.
Made with β€οΈ by Furkan SarΔ±kaya
For detailed documentation, advanced usage patterns, and architectural insights, explore the comprehensive guides in our documentation directory