> ## Documentation Index
> Fetch the complete documentation index at: https://easyaf.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# IServiceCollection

> Extension methods for IServiceCollection from Microsoft.Extensions.DependencyInjection.Abstractions

## Definition

**Assembly:** Microsoft.Extensions.DependencyInjection.Abstractions.dll

**Namespace:** Microsoft.Extensions.DependencyInjection

## Syntax

```csharp theme={"dark"}
Microsoft.Extensions.DependencyInjection.IServiceCollection
```

## Summary

This type is defined in Microsoft.Extensions.DependencyInjection.Abstractions.

## Remarks

See [Microsoft documentation](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection) for more information about the rest of the API.

## Methods

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> AddChainedService <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions`</Note>

A Restier-specific method that adds a "service contributor", which has a chance to chain previously registered service instances.
DO NOT use this method outside of a Restier app.

#### Syntax

```csharp theme={"dark"}
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddChainedService<TService>(Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<System.IServiceProvider, TService, TService> factory, Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime = 0) where TService : class
```

#### Parameters

| Name              | Type                                                          | Description                                                                                                                                        |
| ----------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `services`        | `Microsoft.Extensions.DependencyInjection.IServiceCollection` | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection) to register the *TService* with.      |
| `factory`         | `System.Func<System.IServiceProvider, TService, TService>`    | A factory method to create a new instance of service TService, wrapping previous instance."/>.                                                     |
| `serviceLifetime` | `Microsoft.Extensions.DependencyInjection.ServiceLifetime`    | The [ServiceLifetime](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime) of the service being added. |

#### Returns

Type: `Microsoft.Extensions.DependencyInjection.IServiceCollection`
The *services* instance modified with the new *TService* reference.

#### Type Parameters

* `TService` - The service type to register with the [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).

#### Remarks

This process is being deprecated. Please DO NOT rely on it for future behavior in your own apps. V2 will properly handle
multiple instances of a registration by firing them in succession.

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> AddChainedService <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions`</Note>

A Restier-specific method that adds a "service contributor", which has a chance to chain previously registered service instances.
DO NOT use this method outside of a Restier app.

#### Syntax

```csharp theme={"dark"}
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddChainedService<TService, TImplement>(Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.DependencyInjection.ServiceLifetime serviceLifetime = 0) where TService : class where TImplement : class, TService
```

#### Parameters

| Name              | Type                                                          | Description                                                                                                                                        |
| ----------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `services`        | `Microsoft.Extensions.DependencyInjection.IServiceCollection` | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection) to register the *TService* with.      |
| `serviceLifetime` | `Microsoft.Extensions.DependencyInjection.ServiceLifetime`    | The [ServiceLifetime](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime) of the service being added. |

#### Returns

Type: `Microsoft.Extensions.DependencyInjection.IServiceCollection`
Current [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection)

#### Type Parameters

* `TService` - The service type to register with the [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).
* `TImplement` - The implementation type.

#### Remarks

This process is being deprecated. Please DO NOT rely on it for future behavior in your own apps. V2 will properly handle
multiple instances of a registration by firing them in succession.

If want to cutoff previous registration, not define a property with type of TService or do not use it.
The contributor added will get an instance of *TImplement* from the container, i.e.
[IServiceProvider](https://learn.microsoft.com/dotnet/api/system.iserviceprovider), every time it's get called.
This method will try to register *TImplement* as a service with
[Transient](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime.transient) life time, if it's not yet registered. To override, you can
register *TImplement* before or after calling this method.

Note: When registering *TImplement*, you must NOT give it a
[ServiceLifetime](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime) that makes it outlives *TService*, that could possibly
make an instance of *TImplement* be used in multiple instantiations of
*TService*, which leads to unpredictable behaviors.

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> AddEF6ProviderServices <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.RestierEntityFrameworkServiceCollectionExtensions`</Note>

This method is used to add entity framework providers service into container.

#### Syntax

```csharp theme={"dark"}
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddEF6ProviderServices<TDbContext>(Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TDbContext : System.Data.Entity.DbContext
```

#### Parameters

| Name       | Type                                                          | Description                                                                                                   |
| ---------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| `services` | `Microsoft.Extensions.DependencyInjection.IServiceCollection` | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection). |

#### Returns

Type: `Microsoft.Extensions.DependencyInjection.IServiceCollection`
Current [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).

#### Type Parameters

* `TDbContext` - The DbContext type.

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> AddEFCoreProviderServices <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.RestierEntityFrameworkServiceCollectionExtensions`</Note>

This method is used to add entity framework providers service into container.

#### Syntax

```csharp theme={"dark"}
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddEFCoreProviderServices<TDbContext>(Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = null) where TDbContext : Microsoft.EntityFrameworkCore.DbContext
```

#### Parameters

| Name                                                                                                                                                                                    | Type                                                                                            | Description                                                                                                   |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| `services`                                                                                                                                                                              | `Microsoft.Extensions.DependencyInjection.IServiceCollection`                                   | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection). |
| `optionsAction`                                                                                                                                                                         | `System.Action<System.IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>` | An optional action to configure the Microsoft.EntityFrameworkCore.DbContextOptions                            |
| for the context. This provides an alternative to performing configuration of                                                                                                            |                                                                                                 |                                                                                                               |
| the context by overriding the Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)                                              |                                                                                                 |                                                                                                               |
| method in your derived context.                                                                                                                                                         |                                                                                                 |                                                                                                               |
| If an action is supplied here, the Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)                                         |                                                                                                 |                                                                                                               |
| method will still be run if it has been overridden on the derived context. Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder) |                                                                                                 |                                                                                                               |
| configuration will be applied in addition to configuration performed here.                                                                                                              |                                                                                                 |                                                                                                               |
| In order for the options to be passed into your context, you need to expose a                                                                                                           |                                                                                                 |                                                                                                               |
| constructor on your context that takes Microsoft.EntityFrameworkCore.DbContextOptions\`1                                                                                                |                                                                                                 |                                                                                                               |
| and passes it to the base constructor of Microsoft.EntityFrameworkCore.DbContext.                                                                                                       |                                                                                                 |                                                                                                               |

#### Returns

Type: `Microsoft.Extensions.DependencyInjection.IServiceCollection`
Current [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).

#### Type Parameters

* `TDbContext` - The DbContext type.

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> HasService <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions`</Note>

Return true if the [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection) has any *TService* service registered.

#### Syntax

```csharp theme={"dark"}
public static bool HasService<TService>(Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class
```

#### Parameters

| Name       | Type                                                          | Description                                                                                                                                   |
| ---------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `services` | `Microsoft.Extensions.DependencyInjection.IServiceCollection` | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection) to register the *TService* with. |

#### Returns

Type: `bool`
A [Boolean](https://learn.microsoft.com/dotnet/api/system.boolean) specifying whether or not the *TService*

#### Type Parameters

* `TService` - The service type to register with the [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).

### <Icon icon="puzzle-piece" iconType="duotone" color="#419AC5" size={24} className="mr-2" /> HasServiceCount <Badge color="green">Extension</Badge>

<Note>Extension method from `Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions`</Note>

Returns the number of services that match the given [ServiceType](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicedescriptor.servicetype) in a given [ServiceCollection](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.servicecollection).

#### Syntax

```csharp theme={"dark"}
public static int HasServiceCount<TService>(Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class
```

#### Parameters

| Name       | Type                                                          | Description                                                                                                                                   |
| ---------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `services` | `Microsoft.Extensions.DependencyInjection.IServiceCollection` | The [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection) to register the *TService* with. |

#### Returns

Type: `int`
An [Int32](https://learn.microsoft.com/dotnet/api/system.int32) representing the number of Services that match the given ServiceType.

#### Type Parameters

* `TService` - The service type to register with the [IServiceCollection](/restier/api-reference/Microsoft/Extensions/DependencyInjection/IServiceCollection).
