# ServiceProvider

Namespace: **Wisej.Services**

Assembly: **Wisej.Framework** (4.0.0.0)

Manages services for Wisej.NET's DI implementation.

{% tabs %}
{% tab title="C#" %}

```csharp
public class ServiceProvider : IServiceProvider
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class ServiceProvider
    Inherits IServiceProvider
```

{% endtab %}
{% endtabs %}

## Methods

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService\<TService>(lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The service to add.                                                                                              |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService(serviceType, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService\<TService, TImplementation>(lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **TImplementation**                           |                                                                                  | The type of the implementation to use.                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService(serviceType, implementationType, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationType**                        | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the implementation to use.                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationType* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService\<TService>(implementationTypeName, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **implementationTypeName**                    | [String](https://docs.microsoft.com/dotnet/api/system.string)                    | The name of the type of the implementation; can be a fully qualified assembly + type name.                       |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

Using this overload allows the application to register a service implementation that is loaded dynamically when requested. If *implementationTypeName* is a fully qualified type name, including the assembly name, the assembly and type are loaded on demand. **Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationTypeName* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService(serviceType, implementationTypeName, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationTypeName**                    | [String](https://docs.microsoft.com/dotnet/api/system.string)                    | The name of the type of the implementation; can be a fully qualified assembly + type name.                       |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

Using this overload allows the application to register a service implementation that is loaded dynamically when requested. If *implementationTypeName* is a fully qualified type name, including the assembly name, the assembly and type are loaded on demand. This non-generic method supports the registration of generic services:

```csharp

![
  Application.Services.AddService(typeof(DBConnection<>));
  ...
  var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
  var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationTypeName* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService\<TService>(serviceImplementation, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The service to add.                                                                                              |
| **serviceImplementation**                     | [Object](https://docs.microsoft.com/dotnet/api/system.object)                    | Implementation for the *TService* .                                                                              |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceImplementation* is null.
* [InvalidCastException](https://docs.microsoft.com/dotnet/api/system.invalidcastexception) *serviceImplementation* cannot be assigned to *TService* .
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService(serviceType, serviceImplementation, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The service to add.                                                                                              |
| **serviceImplementation**                     | [Object](https://docs.microsoft.com/dotnet/api/system.object)                    | Implementation for the *serviceType* .                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceImplementation* is null.
* [InvalidCastException](https://docs.microsoft.com/dotnet/api/system.invalidcastexception) *serviceImplementation* cannot be assigned to *serviceType* .
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService\<TService>(implementationFactory, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **implementationFactory**                     | [Func\<Type, Object>](https://docs.microsoft.com/dotnet/api/system.func-2)       | Method for creating the *TService* instance.                                                                     |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *implementationFactory* is null.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddOrReplaceService(serviceType, implementationFactory, lifetime)

Registers or replaces a scoped service. <mark style="color:blue;background-color:green;">Since 4.0.0</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationFactory**                     | [Func\<Type, Object>](https://docs.microsoft.com/dotnet/api/system.func-2)       | Method for creating the *serviceType* instance.                                                                  |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *serviceType* is null.
* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *implementationFactory* is null.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService\<TService>(lifetime)

Registers a scoped service.

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The service to add.                                                                                              |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService(serviceType, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 3.5.4</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService\<TService, TImplementation>(lifetime)

Registers a scoped service.

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **TImplementation**                           |                                                                                  | The type of the implementation to use.                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService(serviceType, implementationType, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 3.5.4</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationType**                        | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the implementation to use.                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationType* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService\<TService>(implementationTypeName, lifetime)

Registers a scoped service.

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **implementationTypeName**                    | [String](https://docs.microsoft.com/dotnet/api/system.string)                    | The name of the type of the implementation; can be a fully qualified assembly + type name.                       |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

Using this overload allows the application to register a service implementation that is loaded dynamically when requested. If *implementationTypeName* is a fully qualified type name, including the assembly name, the assembly and type are loaded on demand. **Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationTypeName* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService(serviceType, implementationTypeName, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 3.5.4</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationTypeName**                    | [String](https://docs.microsoft.com/dotnet/api/system.string)                    | The name of the type of the implementation; can be a fully qualified assembly + type name.                       |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

Using this overload allows the application to register a service implementation that is loaded dynamically when requested. If *implementationTypeName* is a fully qualified type name, including the assembly name, the assembly and type are loaded on demand. This non-generic method supports the registration of generic services:

```csharp

![
  Application.Services.AddService(typeof(DBConnection<>));
  ...
  var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
  var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null or empty.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *implementationTypeName* is null or empty.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService\<TService>(serviceImplementation, lifetime)

Registers a scoped service.

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The service to add.                                                                                              |
| **serviceImplementation**                     | [Object](https://docs.microsoft.com/dotnet/api/system.object)                    | Implementation for the *TService* .                                                                              |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceImplementation* is null.
* [InvalidCastException](https://docs.microsoft.com/dotnet/api/system.invalidcastexception) *serviceImplementation* cannot be assigned to *TService* .
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService(serviceType, serviceImplementation, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 3.5.4</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The service to add.                                                                                              |
| **serviceImplementation**                     | [Object](https://docs.microsoft.com/dotnet/api/system.object)                    | Implementation for the *serviceType* .                                                                           |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceImplementation* is null.
* [InvalidCastException](https://docs.microsoft.com/dotnet/api/system.invalidcastexception) *serviceImplementation* cannot be assigned to *serviceType* .
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService\<TService>(implementationFactory, lifetime)

Registers a scoped service.

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **TService**                                  |                                                                                  | The type of the service to add.                                                                                  |
| **implementationFactory**                     | [Func\<Type, Object>](https://docs.microsoft.com/dotnet/api/system.func-2)       | Method for creating the *TService* instance.                                                                     |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *implementationFactory* is null.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *TService* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) AddService(serviceType, implementationFactory, lifetime)

Registers a scoped service. <mark style="color:blue;background-color:green;">Since 3.5.4</mark>

| Parameter                                     | Type                                                                             | Description                                                                                                      |
| --------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **serviceType**                               | [Type](https://docs.microsoft.com/dotnet/api/system.type)                        | The type of the service to add.                                                                                  |
| **implementationFactory**                     | [Func\<Type, Object>](https://docs.microsoft.com/dotnet/api/system.func-2)       | Method for creating the *serviceType* instance.                                                                  |
| **lifetime** ![](/files/1vGqtbG2j637vQ4ROWOf) | [ServiceLifetime](/api/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

This non-generic method supports the registration of generic services:

```csharp

![
    Application.Services.AddService(typeof(DBConnection<>));
    ...
    var dbTrucks = Application.Services.GetService<DBConnection<Truck>>);
    var dbEmployees = Application.Services.GetService<DBConnection<Employee>>);

```

**Throws:**

* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *serviceType* is null.
* [NullReferenceException](https://docs.microsoft.com/dotnet/api/system.nullreferenceexception) *implementationFactory* is null.
* [InvalidOperationException](https://docs.microsoft.com/dotnet/api/system.invalidoperationexception) A service of type *serviceType* is already registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) GetService\<T>()

Returns the requested service.

| Parameter | Type | Description                      |
| --------- | ---- | -------------------------------- |
| **T**     |      | Type of the service to retrieve. |

**Returns:** [T](/api/wisej.services/general/wisej.services.serviceprovider.md). An instance of *T* or null if the service is not found.

### ![](/files/hsR4ok3152WyAf8J2C1u) GetService(serviceType)

Returns the requested service.

| Parameter       | Type                                                      | Description                 |
| --------------- | --------------------------------------------------------- | --------------------------- |
| **serviceType** | [Type](https://docs.microsoft.com/dotnet/api/system.type) | Type of the service to add. |

**Returns:** [Object](https://docs.microsoft.com/dotnet/api/system.object). An instance of *serviceType* or null if the service is not found.

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null.

### ![](/files/hsR4ok3152WyAf8J2C1u) HasService\<T>()

Checks if a service of type *T* is already registered. <mark style="color:blue;background-color:green;">Since 3.5.12</mark>

| Parameter | Type | Description            |
| --------- | ---- | ---------------------- |
| **T**     |      | Service type to check. |

**Returns:** [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean). true if the service is registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) HasService(serviceType)

Checks if a service of type *serviceType* is already registered. <mark style="color:blue;background-color:green;">Since 3.5.12</mark>

| Parameter       | Type                                                      | Description            |
| --------------- | --------------------------------------------------------- | ---------------------- |
| **serviceType** | [Type](https://docs.microsoft.com/dotnet/api/system.type) | Service type to check. |

**Returns:** [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean). true if the service is registered.

### ![](/files/hsR4ok3152WyAf8J2C1u) Inject(target)

Injects the services into the properties marked with the [InjectAttribute](/api/wisej.services/general/wisej.services.injectattribute.md) attributed on the specified *target* object.

| Parameter  | Type                                                          | Description                                |
| ---------- | ------------------------------------------------------------- | ------------------------------------------ |
| **target** | [Object](https://docs.microsoft.com/dotnet/api/system.object) | Target object to inject dependencies into. |

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *target* is null.

### ![](/files/hsR4ok3152WyAf8J2C1u) RemoveService\<TService>()

Removes the specified service type from the service container.

| Parameter    | Type | Description            |
| ------------ | ---- | ---------------------- |
| **TService** |      | The service to remove. |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

### ![](/files/hsR4ok3152WyAf8J2C1u) RemoveService(serviceType)

Removes the specified service type from the service container.

| Parameter       | Type                                                      | Description                    |
| --------------- | --------------------------------------------------------- | ------------------------------ |
| **serviceType** | [Type](https://docs.microsoft.com/dotnet/api/system.type) | Type of the service to remove. |

**Returns:** [ServiceProvider](/api/wisej.services/general/wisej.services.serviceprovider.md).

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception) *serviceType* is null.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/api/wisej.services/general/wisej.services.serviceprovider.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
