ServiceSharp 2.1.0

dotnet add package ServiceSharp --version 2.1.0
                    
NuGet\Install-Package ServiceSharp -Version 2.1.0
                    
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="ServiceSharp" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ServiceSharp" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ServiceSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ServiceSharp --version 2.1.0
                    
#r "nuget: ServiceSharp, 2.1.0"
                    
#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.
#addin nuget:?package=ServiceSharp&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ServiceSharp&version=2.1.0
                    
Install as a Cake Tool

ServiceSharp

ServiceSharp v2.1.0 is released with .NET Standard 2.1 support and new features!
v2.0.0 was released with .NET 6 and rewritten from scratch! You no longer need the ASP.NET Core specific package as well.

ServiceSharp is a tiny utility for registering services in .NET Dependency Injection (DI) using [Attribute], [SelfService], or Interface:

Declare using Attributes

[Service(typeof(ITestAttr1))] // Explicit declaration
class ImplAttr1 : DefaultImplementation, ITestAttr1 { }

[Ignore]
interface IShouldIgnore {} // Ignore this interface unless explicitly declared

[Service] // Automatically register all implemented interfaces
class ImplAttr23 : 
    ITestAttr2, ITestAttr3,
    IShouldIgnore // Ignored unless explicitly registered
{ }

[Service(typeof(ITestAttr4)), // Multiple explicit declarations
 Service(typeof(ITestAttr5))] // Multiple explicit declarations
class ImplAttr456 : DefaultImplementation, ITestAttr4, ITestAttr5, ITestAttr6 { }

[Service(Lifetime = ServiceLifetime.Scoped)] // Lifetime can be specified
class ImplScoped : ITestAttrScoped { }

[Service(Lifetime = ServiceLifetime.Transient)]
class ImplTransient : ITestAttrTransient { }

[Service(Lifetime = ServiceLifetime.Singleton)]
class ImplSingleton : ITestAttrSingleton { }

[SelfService] // Register the class as itself (not by interface)
class SelfServiceClass { }

[SelfService] // Register as itself, not by interface
class ImplSelfServiceWithInterface : ISelfServiceInterface { }

Declare using Interfaces

class ImplInterface1 :
    ITestInterface1,
    ITestInterface2,
    ITestInterface3,           // This service is not registered
    IService<ITestInterface1>, // Explicit declaration
    IService<ITestInterface2>  // Explicit declaration
{ }

[Ignore]
interface IShouldIgnore {} // Ignore this interface unless explicitly declared

// Use Interface declaration for all implemented interfaces
class ImplInterface23 : 
    IService, // Automatically register all implemented interfaces
    ITestInterface2,
    ITestInterface3,
    IShouldIgnore // Ignored unless explicitly registered
{ }

[Ignore] // Do not register this class though it implements IService interface
class ImplInterface3 : 
    ITestInterface3,
    IService
{ }

Note
The lifetime of the service cannot be specified using interface declaration. It uses the value from options instead. The default value is Scoped.

New in 2.1.0
Use [SelfService] to register a class as itself (not by interface). If the class also implements interfaces, those interfaces are not registered unless you use [Service] or interface-based registration.

Installation

You can install from Nuget Package:

Install-Package ServiceSharp

Note
The old package for ASP.NET Core is now deprecated. You don't need it anymore.

Usage

Use AddServices to register all the declared services in the Assembly:

services.AddServices();

You can also optionally specify a few options:

services.AddServices(options => {
    // Change scanning Assembly,
    // the default is Assembly.GetCallingAssembly() which should be your own code
    options.Assembly = Assembly.GetExecutingAssembly();

    // Add more assemblies to be scanned in case you have multiple assemblies
    options.AdditionalAssemblies.Add(GetMyAssembly());

    // Set the default Lifetime for the services if not specified by Attribute declaration
    // The default value is Scoped
    options.Lifetime = ServiceLifetime.Scoped;

    // You can also make and specify your own scanner
    options.TypeScanner = new MyTypeScanner();
});```
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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 (1)

Showing the top 1 NuGet packages that depend on ServiceSharp:

Package Downloads
ServiceSharp.AspNetCore

A tiny utility for Repository-Service pattern for ASP.NET Core

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 134 6/24/2025
2.0.0 263 7/24/2023
1.0.2 1,399 7/6/2018
1.0.1 1,283 7/6/2018
1.0.0 1,107 7/6/2018