https://github.com/datvm/servicesharp
ServiceSharp is a tiny utility for registering services in .NET DI using [Attribute] or Interface
https://github.com/datvm/servicesharp
Last synced: 6 months ago
JSON representation
ServiceSharp is a tiny utility for registering services in .NET DI using [Attribute] or Interface
- Host: GitHub
- URL: https://github.com/datvm/servicesharp
- Owner: datvm
- License: mit
- Created: 2018-07-06T18:07:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-06-24T04:39:02.000Z (8 months ago)
- Last Synced: 2025-07-24T09:15:34.564Z (7 months ago)
- Language: C#
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.MD
Awesome Lists containing this project
README
# 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
```cs
[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
```cs
class ImplInterface1 :
ITestInterface1,
ITestInterface2,
ITestInterface3, // This service is not registered
IService, // Explicit declaration
IService // 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](https://www.nuget.org/packages/ServiceSharp/):
```ps
Install-Package ServiceSharp
```
> **Note**
> The old package for [ASP.NET Core](https://www.nuget.org/packages/ServiceSharp.AspNetCore/) 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:
```cs
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();
});```