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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DependencyInjection.Extensions.AutoService" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyInjection.Extensions.AutoService --version 1.0.4
#r "nuget: DependencyInjection.Extensions.AutoService, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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.

  1. AddSelfImplementation - Adds implementing class as service.
  2. WhenNoServicePresent - Adds implementing class as service only when no other service is auto registered on implementing class.
  3. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 161 8/2/2023
1.0.0 134 7/27/2023

Fixing usage of disabled self implementation