Share via


ServiceCollectionHostedServiceExtensions.AddHostedService Method

Definition

Overloads

AddHostedService<THostedService>(IServiceCollection)

Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs

Add an IHostedService registration for the given type.

public:
generic <typename THostedService>
 where THostedService : class, Microsoft::Extensions::Hosting::IHostedService[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::DependencyInjection::IServiceCollection ^ AddHostedService(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ services);
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHostedService<THostedService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where THostedService : class, Microsoft.Extensions.Hosting.IHostedService;
static member AddHostedService : Microsoft.Extensions.DependencyInjection.IServiceCollection -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)
<Extension()>
Public Function AddHostedService(Of THostedService As {Class, IHostedService}) (services As IServiceCollection) As IServiceCollection

Type Parameters

THostedService

An IHostedService to register.

Parameters

services
IServiceCollection

The IServiceCollection to register with.

Returns

The original IServiceCollection.

Examples

The following code shows how to register a hosted service while also registering the actual THostedService type.

services.AddSingleton<SomeService>();
services.AddHostedService(sp => sp.GetRequiredService<SomeService>());

Remarks

Note that this creates registration for IHostedService specifically. Not for the actual THostedService type. If you want to register the actual type, you must do so separately.

Applies to

AddHostedService<THostedService>(IServiceCollection, Func<IServiceProvider,THostedService>)

Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs
Source:
ServiceCollectionHostedServiceExtensions.cs

Add an IHostedService registration for the given type.

public:
generic <typename THostedService>
 where THostedService : class, Microsoft::Extensions::Hosting::IHostedService[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::DependencyInjection::IServiceCollection ^ AddHostedService(Microsoft::Extensions::DependencyInjection::IServiceCollection ^ services, Func<IServiceProvider ^, THostedService> ^ implementationFactory);
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHostedService<THostedService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Func<IServiceProvider,THostedService> implementationFactory) where THostedService : class, Microsoft.Extensions.Hosting.IHostedService;
static member AddHostedService : Microsoft.Extensions.DependencyInjection.IServiceCollection * Func<IServiceProvider, 'HostedService (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)> -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'HostedService : null and 'HostedService :> Microsoft.Extensions.Hosting.IHostedService)
<Extension()>
Public Function AddHostedService(Of THostedService As {Class, IHostedService}) (services As IServiceCollection, implementationFactory As Func(Of IServiceProvider, THostedService)) As IServiceCollection

Type Parameters

THostedService

An IHostedService to register.

Parameters

services
IServiceCollection

The IServiceCollection to register with.

implementationFactory
Func<IServiceProvider,THostedService>

A factory to create new instances of the service implementation.

Returns

The original IServiceCollection.

Examples

The following code shows how to register a hosted service while also registering the actual THostedService type.

services.AddSingleton<SomeService>(implementationFactory);
services.AddHostedService(sp => sp.GetRequiredService<SomeService>());

Remarks

Note that this creates registration for IHostedService specifically. Not for the actual THostedService type. If you want to register the actual type, you must do so separately.

Applies to