> For the complete documentation index, see [llms.txt](https://docs.wisej.com/api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wisej.com/api/v3.5/wisej.services/general/wisej.services.serviceprovider.md).

# ServiceProvider

Namespace: **Wisej.Services**

Assembly: **Wisej.Framework** (3.5.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)AddService\<TService>(lifetime)

Registers a scoped service.

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

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

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) *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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/wisej.services/general/wisej.services.servicelifetime.md) | Scope of the service. Default is [Shared](/api/v3.5/wisej.services/general/wisej.services.servicelifetime.md#fields). |

**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/v3.5/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)Inject(target)

Injects the services into the properties marked with the [InjectAttribute](/api/v3.5/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(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. |

**Throws:**

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
