DependencyInjection.Extensions.AutoService
1.0.4
dotnet add package DependencyInjection.Extensions.AutoService --version 1.0.4
NuGet\Install-Package DependencyInjection.Extensions.AutoService -Version 1.0.4
<PackageReference Include="DependencyInjection.Extensions.AutoService" Version="1.0.4" />
paket add DependencyInjection.Extensions.AutoService --version 1.0.4
#r "nuget: DependencyInjection.Extensions.AutoService, 1.0.4"
// Install DependencyInjection.Extensions.AutoService as a Cake Addin
#addin nuget:?package=DependencyInjection.Extensions.AutoService&version=1.0.4
// Install DependencyInjection.Extensions.AutoService as a Cake Tool
#tool nuget:?package=DependencyInjection.Extensions.AutoService&version=1.0.4
Auto register service in Microsoft.DI using attribute.
// Service registration without attribute
services.AddScoped<IServiceA, Service>()
.AddScoped<IServiceB, Service>();
// Service registration alternative using attribute
[AutoService]
public class Service : IServiceA, IServiceB
Setup
Decorate classes
// Decorate class as auto service
[AutoService]
public class Service : IServiceA, IServiceB
Add configuration in startup.cs
private static void ConfigureServices(IServiceCollection services)
{
var executingAssembly = Assembly.GetExecutingAssembly()
// Add AutoServices
services.AddAutoServices(executingAssembly);
// optionally register all auto services from calling assembly
// services.AddAutoServices();
// Add other registration
}
Sample usages
Description SelfImplementationUsage
Enum class describing how to handle self registration, which means that service implementation serves as service.
- AddSelfImplementation - Adds implementing class as service.
- WhenNoServicePresent - Adds implementing class as service only when no other service is auto registered on implementing class.
- Disabled - Implementing class is never added as service.
Using all implemented interfaces
By default attribute registers all implemented interfaces with Scoped lifetime and add self implementation only when no interface is implemented.</br> Other self implementation options are described here.
public AutoServiceAttribute(SelfImplementationUsage selfImplementation = SelfImplementationUsage.WhenNoServicePresent, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
Examples
Register all implemented interfaces with lifetime as scoped.
Implementation
abstract class BaseService: IServiceBase
[AutoService]
class Service : IServiceA, IServiceB
[AutoService]
class ChildService : BaseService
Registered services
ServiceType | Implementation | Lifetime |
---|---|---|
IServiceA | Service | Scoped |
IServiceB | Service | Scoped |
IServiceBase | ChildService | Scoped |
Register self implementation with lifetime as scoped.
Implementation
// SelfImplementationUsage.WhenNoServicePresent
[AutoService]
class Service
[AutoService(SelfImplementationUsage.AddSelfImplementation)]
class SelfService : IServiceA, IServiceB
[AutoService(SelfImplementationUsage.Disabled)]
class NoSelfService
Registered services
ServiceType | Implementation | Lifetime |
---|---|---|
Service | Service | Scoped |
IServiceA | SelfService | Scoped |
IServiceB | SelfService | Scoped |
SelfService | SelfService | Scoped |
Register services with different lifetimes
Implementation
[AutoService(serviceLifetime: ServiceLifetime.Transient)]
class Service : IServiceA, IServiceB
Registered services
ServiceType | Implementation | Lifetime |
---|---|---|
IServiceA | Service | Transient |
IServiceB | Service | Transient |
Using explicit types
By default attribute registers with lifetime scoped.
public AutoServiceAttribute(Type serviceType, params Type[] servicesTypes)
public AutoServiceAttribute(ServiceLifetime serviceLifetime, Type serviceType, params Type[] servicesTypes)
Examples
Register explicitly defined service types
Implementation
[AutoService(typeof(IServiceA)]
class Service : IServiceA, IServiceB
[AutoService(typeof(IServiceB), typeof(ServiceB)]
class ServiceB : IServiceA, IServiceB
Registered services
ServiceType | Implementation | Lifetime |
---|---|---|
IServiceA | Service | Scoped |
IServiceB | ServiceB | Scoped |
ServiceB | ServiceB | Scoped |
Register explicitly defined with different lifetimes
Implementation
[AutoService(ServiceLifetime.Transient, typeof(IServiceA), typeof(Service)]
[AutoService(ServiceLifetime.Singleton, typeof(IServiceB)]
class Service : IServiceA, IServiceB
Registered services
ServiceType | Implementation | Lifetime |
---|---|---|
IServiceA | Service | Transient |
Service | Service | Transient |
IServiceB | Service | Singleton |
Wrong usages
Attribute cannot be used on abstract class.</br> Using explicitly named types has to be assignable from decorated type.
Abstract class example
[AutoService]
abstract class Service
Not implementing service example
[AutoService(typeof(IServiceA)]
class Service
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixing usage of disabled self implementation